First of all, I’m using Rails 3.1.1 and Ruby 1.9.3-p0. After experimenting for a few hours on my existing project and a new “sanity check” project, I can confirm that this doesn’t seem to work. I’ve tried in both HAML and ERB but here is the HAML version. First the layout file:
/ application.html.haml
- content_for :stuff, "some content"
#content
= yield # lets say we are in the show action
Followed by the action view:
/ show.html.haml
= content_for :stuff
The result is that nothing gets displayed. I hope I am extremely wrong of course because I really need to be able to do this and it seems like this is one of those common scenarios. Shouldn’t this work or no? If not, could I get a suggestion on a good alternative? Thanks!
So I went through the source code and I discovered that this is actually not possible because templates are rendered before the layout file. I have yet to come up with an alternative solution.