我如何使用 content_for 在 :yield 中放入一些东西 [英] How can I use content_for to put something in :yield
问题描述
我使用的是 ruby 1.9.2、rails3.
I am in ruby 1.9.2, rails3.
所以我的网站有一些结构,
So My website has some structures,
我想把菜单放在我的网页中间.
and I want to put menu in a middle of my webpage.
我正在做类似的事情(在 application.html.erb 文件中)
I am doing something like (within application.html.erb file)
blahblahblah
<div id="menu">
<%= yield :menu %>
<div>
blahblhablah
我有一个文件 menu.html.erb,其中包含该站点的菜单结构.如果我想使用 ./layout 文件夹中的文件作为该 yield :menu 的一部分,我该怎么办?我想知道,是否必须为每个控制器以及每个函数使用 content_for ......顺便说一句,每个控制器的 menu.html.erb 会有所不同,所以这就是我放弃它的原因.
I have a file menu.html.erb which has menu structure for the site. What can I do if I want to use a file within ./layout folder to be used to be part of that yield :menu? I was wondering, if I have to use content_for for every controller, and within every functions... Btw, menu.html.erb will be different for each controller, so thats why I am yielding it.
总而言之,我只想在几乎所有地方都包含一个通用的共享 menu.html.erb.
In conclusion, I just want to include one common shared menu.html.erb pretty much everywhere.
推荐答案
您可以在视图中执行以下操作:
You could do something like this in your views:
<% content_for(:menu) do %>
<%= render :partial => "/layouts/user_menu.html.erb" %>
<% end %>
您可以尝试将其与 controller.controller_name
结合使用(不确定这是否适用于 Rails3)并自动为每个控制器加载不同的菜单.
You could try to combine this with controller.controller_name
(not sure this works for Rails3) and load a different menu for each controller automatically.
这篇关于我如何使用 content_for 在 :yield 中放入一些东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!