![](/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.