imjp
imjp

Reputation: 6695

How do I only display a specific layout for Controller X

Let's say I only want to display a specific layout when I'm on the home page. The home page's controller and action is home#index.

How do I display a different layout for business#new?

Upvotes: 0

Views: 59

Answers (2)

Kirti Thorat
Kirti Thorat

Reputation: 53038

There are couple of options:

Option 1 - Specific Layout at Controller level

Just create a layout file named business.html.erb in app/views/layouts and it will be automatically picked for rendering ALL the views of BusinessController

Option 2 - Custom layout name at Controller level

Create a layout file named my_custom_name.html.erb in app/views/layouts and in your controller specify it as:

class BusinessController < ApplicationController
  layout "my_custom_name"
  #...
end

my_custom_name layout would be used for ALL the views corresponding to BusinessController.

Option 3 - Layout for a particular action in a Controller

Create a layout file named layout_name.html.erb in app/views/layouts and in your controller specify it as:

class BusinessController < ApplicationController

   def new
     # ...
     render :layout => "layout_name"
   end 
end

In this case, layout_name layout would only be applied for rendering new.html.erb page.

Upvotes: 1

Ignacio
Ignacio

Reputation: 21

you can specify the layout of a controller with

class businessController < ApplicationController
  layout "business_layout"
  #...
end

http://guides.rubyonrails.org/layouts_and_rendering.html 2.2.14 Finding Layouts

Upvotes: 0

Related Questions