Mark Locklear
Mark Locklear

Reputation: 5325

Rails 4: set layout equal to false in controller

In a Rails 4 app I am trying to set the layout to false or nil. I tried this from inside my controller:

render :layout => false

But that gives this error:

undefined method `render'.

How can I stop this controller from using the default layout file?

Upvotes: 5

Views: 4941

Answers (3)

vee
vee

Reputation: 38645

To disable layout for a controller:

class FooController < ApplicationController
  layout false
  ...
end

Upvotes: 8

Kirti Thorat
Kirti Thorat

Reputation: 53018

Scenario 1: To disable layout for all the actions of a controller use it as:

class FoosController < ApplicationController
   layout false  ## Note it is not within any action

   def create
   ...
   end

...
end

Scenario 2: To disable layout for a specific action of a controller use it as:

class FoosController < ApplicationController
  ...
  def show
   ...
   render layout: false
  end
  ...
end

Upvotes: 6

craig.kaminsky
craig.kaminsky

Reputation: 5598

I believe what you are looking for is to render nothing for a particular method (action) in a controller. This should work based on the output you linked to in the pastie under @vee's answer:

def new
  # code for the sessions#new controller#method
  render nothing: true
end

EDIT: This answer assumes that you do not want to render your actual view template ... if you really do not want to render a layout (versus a view template), @vee's response is the one.

Upvotes: 0

Related Questions