views:

22

answers:

1

I'm learning RoR through this tutorial. In tute, an application.html.erb file is created in views/layouts. In the body, a content div is created, and the <%= yield %> line is used. The other views that are created are then just inserted into the content div in the body of the application template.

But there are a few views that I have where I need to add some javascript to the head. My question is, how can I use this structure, with a central application template, but also be able to put code into the head of the document? Thanks for reading.

+3  A: 

Add <%=yield :header %> in your HTML head. Then, in a view:

<% content_for :header do %>
  <!-- Javascript here //-->
<% end %>

<!-- Rest of the page here //-->

The bit in the content_for block will render where your yield :header is in your template.

Chris Heald