views:

405

answers:

3

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!

A: 

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.

danengle
The 'news.html.erb' file to which I referred doesn't actually exist in my project; I was just continuing the from the example shown in the Rails Guides (see link in original message). The problem here is not to create a general layout for all my views; the problem is using a nested layout within the existing application.html.erb file. If you'll take a look at the references article, the idea is explained in more detail.Again, to clarify, I have a application.html.erb nested with a second layout file. I would like the combination of the nested layouts to appear for multiple controllers.
A: 

"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
MattMcKnight
Again, I think the central question is being lost. I have a standard application layout that is being used on all pages, and I have a secondary layout that I'm using with it for nested layouts. What i would like is for all nested resources within one particular resource to use the nested layout template. Thanks!
That's exactly what this would do...
MattMcKnight
+1  A: 

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