[英]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
. 主页的控制器和操作是
home#index
。
How do I display a different layout for business#new
? 如何为
business#new
显示不同的布局?
There are couple of options: 有两种选择:
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
只需在
app/views/layouts
创建一个名为business.html.erb
的布局文件,它将被自动选中以呈现BusinessController
所有视图
Create a layout file named my_custom_name.html.erb
in app/views/layouts
and in your controller specify it as: 在
app/views/layouts
创建一个名为my_custom_name.html.erb
的布局文件,并在您的控制器中将其指定为:
class BusinessController < ApplicationController
layout "my_custom_name"
#...
end
my_custom_name
layout would be used for ALL the views corresponding to BusinessController
. my_custom_name
布局将用于与BusinessController
对应的所有视图。
Create a layout file named layout_name.html.erb
in app/views/layouts
and in your controller specify it as: 在
app/views/layouts
创建一个名为layout_name.html.erb
的布局文件,并在您的控制器中将其指定为:
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. 在这种情况下,
layout_name
布局将仅应用于呈现new.html.erb
页面。
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 http://guides.rubyonrails.org/layouts_and_rendering.html 2.2.14查找布局
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.