2017-06-22 202 views
0

是否可以根據整個應用中的條件呈現不同的模板?Rails渲染模板

我不希望在每個視圖水木清華寫這樣

if domain =='cool' 
    render template 'cool/index' 
else 
    regular template 
end 

我想我需要爲它

回答

2

你可以在你的ApplicationController像這樣實現這一目標做application controller東西。通過將一個符號傳遞給佈局方法,它允許您動態地將佈局分配給應用程序中的所有控制器。

class ApplicationController 
    layout :special_layout 

    private 
    def special_layout 
     (domain =='cool') ? "cool" : "not_cool" 
    end 

end