Have you ever wished you could call yield multiple times in the same layout in order to get extra stylesheets, javascript, or a dynamic sidebar? Having worked on several projects that needed this soft of functionality, I’ve probably built this in three or four times. Then I started watching railscasts.
There’s a method called content_for that provides this functionality. All you have to do is call content for in your view. Here’s the erb markup:
<% content_for :head do %> <%= stylesheet_link_tag :my_style %> <% end %>
Then, in your layout all you have to do is call yield like you normally would in your layout with on small difference. Here’s the erb markup.
1 | <%= yield :head %> |
I picked up this tip from Ryan Bates Railscast Episode #8







No Comments »
No comments yet.
Leave a comment