views:

44

answers:

1

Suppose I have an application layout, and in their I yield for :head content as follows:

<head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <title></title>
    <%= stylesheet_link_tag 'scaffold' %>
    <%= yield(:head) %>
</head>

Suppose in that same application layout I call a partial to render the main menu.

<body>

    <p style="color: green"><%= flash[:notice] %></p>

    <!-- Main Menu -->
    <%= render :partial => 'menu/menu_main' %>
</body>

Is there any way, from within my _menu_main.erb partial to add content to the :head section of my application layout (say to add some css)?

A: 

You have to use the content_for tag.

<% content_for :head do -%>
  *your code here*
<% end -%>

So, the code you put inside will apear on the head of your html file.

This railscasts episode explains it in more details: http://railscasts.com/episodes/8-layouts-and-content-for