简体   繁体   中英

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 ?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM