如果缺少布局文件,如何使用默认布局?
我的文件夹layouts/themes/
包含主题,但如果文件不存在,我想加载默认主题。
我尝试设置rescue_from ActionView:MissingTemplate, with => ...
,但我无法以这种方式设置布局。
所以我找到的最佳解决方案是: layout:layout_by_resource
def layout_by_resource
layout_name = "themes/" + current_theme
File.exists?(Rails.root + "app/views/layouts/" + (layout_name + ".html.erb")) ? layout_name : "application"
end
是否有更好的解决方案用于此目的?
答案 0 :(得分:1)
在Rails中,布局存储在app/views/layouts
中,默认布局名称为application.html.erb
,如果没有布局的名称与控制器名称相对应,则使用它。例如,照片控制器将具有photos.html.erb
布局。
有关详细信息,请查看here(第2.2.13节)