2009-08-29 32 views
6

將內容添加到佈局假設我有一個應用程序的佈局,並在他們的我產生了:頭內容如下:從部分

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

假設在同一應用程序佈局我叫偏呈現主菜單。

<body> 

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

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

有什麼辦法,從我_menu_main.erb部分內添加內容:我的應用程序佈局的頭部分(比如添加一些CSS)?

回答

5

下面是答案https://stackoverflow.com/a/10226010

在你application.html.erb

<head> 
    <% if content_for? :for_head %> 
    <%= yield :for_head %> 
    <% end %> 

在你的 「具體」 的說法:

<% content_for :for_head do %> 
    Something-to-put-in-head 
<% end %> 

:for_head未預定義:命名由您決定。它可能是任何東西。