Reputation: 10152
How to use default layout if It layout file is missing?
I have folder layouts/themes/
that contains themes, but I want to load default theme if file is not exist.
I tried to set rescue_from ActionView:MissingTemplate, with => ...
but I can't set layout in such way.
So the best solution that I found is: 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
Is there better solution for such purpose?
Upvotes: 0
Views: 3816
Reputation: 14967
In Rails layouts are stored in app/views/layouts
and default layout name is application.html.erb
and it is used if there is no layout that's name correspond with controller name. For example, photos controller would have photos.html.erb
layout.
For more details look here (section 2.2.13)
Upvotes: 1