Trouble getting Ruby on Rails yield to work in the UI layer
I am following this tutorial http://guides.rubyonrails.org/layouts_and_rendering.html and trying to separate the header div from the rest of the HTML templates.
I have taken out a div from my app/views/home/index.html.erb file 开发者_开发问答and replaced it with
<%= yield :head %>
Then I made a file called head.html.erb in the /app/views/layouts directory and put that div in there. Then I put this code around the div:
<% content_for :head do %>
<% end %>
But it doesn't work and the header div does not get displayed. Where did I go wrong? I think there is a path and directory mismatch, but I am not sure exactly how to match them up.
Thank you!
Your layout is where you put the yield
call, and each view's template is where you put the content_for
block. You'll probably want your yield
call in your application's main layout file.
So in app/views/layouts/application.html.erb
put this where you want it to be:
<div id="my_header_div">
<%= yield :head %>
</div>
Then in each view, you can do this:
<% content_for :head do %>
<h1>My Header!</h1>
<% end %>
And it will insert inside the div in your layout file.
You should put this:
<%= yield :head %>
in your layout file
If your head
layout is not layout for your particular action content_for :head
will never work
精彩评论