开发者

How can I use content_for to put something in :yield

开发者 https://www.devze.com 2023-01-20 13:20 出处:网络
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.

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.

I am doing something like (within application.html.erb file)

blahblahblah
<div id="menu">
   <%= yield :menu %>
<div>
blahblhablah

I have a file menu.h开发者_高级运维tml.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.

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 %>

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.


You might consider watching the railscast on layouts, it's concise and helpful. Numbers 7 and 8.

http://railscasts.com/episodes?search=layout

0

精彩评论

暂无评论...
验证码 换一张
取 消