假设我有一个应用程序布局,并且在其中我产生了:head内容,如下所示:
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title></title>
<%= stylesheet_link_tag 'scaffold' %>
<%= yield(:head) %>
</head>
假设在相同的应用程序布局中,我称局部 View 来渲染主菜单。
<body>
<p style="color: green"><%= flash[:notice] %></p>
<!-- Main Menu -->
<%= render :partial => 'menu/menu_main' %>
</body>
有什么办法可以从_menu_main.erb部分中向我的应用程序布局的:head部分添加内容(例如添加一些CSS)?
最佳答案
您必须使用 content_for 标记。
<% content_for :head do -%>
*your code here*
<% end -%>
因此,您放入其中的代码将显示在html文件的头上。
这个railscasts情节更详细地说明了这一点:http://railscasts.com/episodes/8-layouts-and-content-for
关于ruby-on-rails - 从局部添加内容到布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1352337/