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
- Marble Paint
(Friday 02/4/2011 – 1 Comment) - More Flickr Original Updates
(Sunday 01/23/2011 – 13 Comments) - Flickr Original updates
(Saturday 08/7/2010 – 19 Comments) - LED Light for iPhone 4
(Monday 06/28/2010 – 65 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