Reputation: 936
Is it possible to automatically assign a specified layout template to a particular controller and all of the resources nested within it, as specified in routes.rb? This layout should apply only the specified controller views and those nested within it; it does not need to be applied to every view within the application, as application.html.erb would (I'm actually using the specialized layout with the application layout for a nested layout).
So, for example, if I had
map.resources :news, :shallow => true do |n|
n.resources :articles do |a|
a.resources :comments
end
end
when I visit a url like localhost/news/1/articles/new
I should see my news.html.erb
layout in action. As of now, I don't.
I obviously don't want to recreate the same layout file for each controller nested within the parent (even if I would pull out the meat the layout and put it in a shared partial). I'm even less thrilled about specifying the layout template in the specific controllers themselves (this specific example is kind of a temporary thing, though I will have a 'real' use-case for this a bit further down the road).
Thanks!
Upvotes: 0
Views: 1415
Reputation: 936
Editing the original question for clarity (the answers were not quite answering the central problem), I realized what I need to do is have the nested controller classes inherit from the top-level parent. Not only does this make solving the central issue easier, it fixes a few other things that have been nagging me.
(I'd say "a'doy" but there are others working on this app, which obscured what would normally be a bit more obvious.)
Upvotes: 1
Reputation: 8290
"I'm even less thrilled about specifying the layout template in the controllers themselves"
There's no reason to worry about this. This is simply what you do. It's one line of DSL code, specifically created for this purpose. Not clunky.
class ArticlesController < ActionController::Base
layout :news
end
Upvotes: 0
Reputation: 566
For your news.html.erb problem, is that a typo? You should see your new.html.erb file that's in your views/articles folder (assuming default layout) and not a news.html.erb file. You'll have to make sure your @news instance variable is set and your form_for will be for [@news, @article] instead of just @article.
Also, you don't have to create a layout for each controller, you can create one application.html.erb in your layouts folder and all controllers that don't have a layout in the layouts folder will use application.html.erb and you don't have to specify it in your controllers, just remove the layout with the same name as the controller.
Upvotes: 0