Layouts Ignored in Rails Controllers

This is probably going to be another one of those three-second fixes, but I've really checked and re-checked all of my code this time. I'm trying to get a custom layout to display in a Rails app, but layout declaration in ApplicationController is not getting propagated to subclasses.

This does not work, meaning I don't get my custom layout:
class ApplicationController < ActionController::Base
  layout 'standard'
end


This does work, however:
class PhotosController < ApplicationController
  layout 'standard'
end


My layout file is in view/layouts/standard.rhtml. I'm just following along in the O'Reilly Rails book on page 74. If I add a "hello world" method with render_text to ApplicationController, it is successfully inherited by PhotosController, so I know the class is being loaded. It just seems be ignoring layout.

Am I missing something obvious? It's not the end of the world if I have to put layout in multiple files, but I'd just like to understand what's going on. Nothing relevant in the log file and I tried Googling for various things, but all the pages I found suggest I'm doing it the right way.

I'm using Locomotive 2.0.8 with "Standard Rails Sept 2006".
Design Element
Layouts Ignored in Rails Controllers
Posted Jan 8, 2007 — 10 comments below




 

Tammer Saleh — Jan 09, 07 3113

This probably isn't it, but do you have a file named app/views/layouts/photos.rhtml? If so, try removing it.

Good luck.

Scott Stevenson — Jan 09, 07 3114 Scotty the Leopard

Well that did it. I'm surprised the explicit call doesn't override the filesystem contents, though.

Scott Matthewman — Jan 09, 07 3115

It's worth noting that if you have a layout named application.rhtml, it'll be used by default with the need for an explicit layout declaration in any controllers.

Scott Matthewman — Jan 09, 07 3116

Gah. Knew I shouldn't post before my first coffee of the morning. Of course, I meant 'without' an explicit declaration.

Chuck — Jan 09, 07 3117

Well that did it. I'm surprised the explicit call doesn't override the filesystem contents, though.

According to the documentation, explicitly setting a layout will override any automatic layout choice for that class, but a parent class's layout will not override a child's layout.

If you want to explicitly extend a class's layout to all of its subclasses, you should be able to do that with the inherited method. The way Rails does it seems most flexible to me, though.

Matt Tavares — Jan 09, 07 3118

are you yeilding your content in the <body> of the layout?
<%= yeild %>

Matt Tavares — Jan 09, 07 3119

Argg, stupid html.

add a <%= yeild %> tag to the <body> tag of your layout (sorry if you've already done this, but this is where my layouts screw up 50% of the time).

Mike — Jan 09, 07 3120

Ff your controller was generated with a scaffold, there will be a 'photo' layout in your layouts folder which takes precedence over your declared layout in ApplicationController

Tammer Saleh — Jan 09, 07 3170

No problem. That recently bit us as well... I find it kind of annoying that the scaffolding creates model.rhml layouts when they're so rarely useful.

BTW: My coworkers and I were really impressed with your blog design.

Richard McIntyre — Feb 28, 07 3664

I just had exactly the same problem,

it worked out that I had a parse error in my layout file and rather than show me the error in this file, rails just defaulted to the layout that scaffold set up for me.

If you delete the scaffold layout file as others have suggested it may help you.




 

Comments Temporarily Disabled

I had to temporarily disable comments due to spam. I'll re-enable them soon.





Copyright © Scott Stevenson 2004-2015