Search code examples
ruby-on-railslayoutnested-resources

Nested Layout for Nested Resources in Rails


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!


Solution

  • 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.)