content_for in Rails
Tuesday 11/18/2008 – Category: Uncategorized
I'm not sure why I hadn't come across this before, but Rails has a nifty helper called content_for that allows you to insert stuff anywhere in your layout.
A classic use case is inserting a javascript source file that is only used on a specific page.
In your view:
<% content_for :head do %> <%= javascript_include_tag 'whatever.js' %> <% end %>
In your layout:
<head> <title>Hello World</title> <%= yield :head %> </head>
One Response to “content_for in Rails”
Leave a Reply
Recent Posts
- Flickr Original updates
(Saturday 08/7/2010 – 7 Comments) - LED Light for iPhone 4
(Monday 06/28/2010 – 45 Comments) - WWDC 2010: Worth Every Minute
(Monday 06/14/2010 – No Comments) - Flickr Original for Safari 5!
(Wednesday 06/9/2010 – 26 Comments)

November 20th, 2008 at 1:25 pm
Yeah, that one is awesome. The best part about it is you can use multiple blocks, so you can put your stylesheets in the :head and all your javascripts in a :foot block