![](/img/trans.png)
[英]How do i use link_to in order to link to an index controller but listing only those objects with a specific attribute?
[英]How do I only display a specific layout for Controller X
假设我只想在主页上显示特定的布局。 主页的控制器和操作是home#index
。
如何为business#new
显示不同的布局?
有两种选择:
只需在app/views/layouts
创建一个名为business.html.erb
的布局文件,它将被自动选中以呈现BusinessController
所有视图
在app/views/layouts
创建一个名为my_custom_name.html.erb
的布局文件,并在您的控制器中将其指定为:
class BusinessController < ApplicationController
layout "my_custom_name"
#...
end
my_custom_name
布局将用于与BusinessController
对应的所有视图。
在app/views/layouts
创建一个名为layout_name.html.erb
的布局文件,并在您的控制器中将其指定为:
class BusinessController < ApplicationController
def new
# ...
render :layout => "layout_name"
end
end
在这种情况下, layout_name
布局将仅应用于呈现new.html.erb
页面。
您可以使用以下命令指定控制器的布局
class businessController < ApplicationController
layout "business_layout"
#...
end
http://guides.rubyonrails.org/layouts_and_rendering.html 2.2.14查找布局
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.