Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails content_for overwrites rather than appends

I load my stylesheets and js files in <head> for performance reasons.

My site has multiple components and each template wants to its own extra header files in inside <% yield(:head).

I tested <% content_for :head do %> .. but then I realize it actually overwrites rather than append to a particular section.

What do you guys use?

like image 317
disappearedng Avatar asked Sep 23 '11 03:09

disappearedng


1 Answers

content_for actually appends by default. From the documentation, if you were to do...

<% content_for :navigation do %>
  <li><%= link_to 'Home', :action => 'index' %></li>
<% end %>

<%#  Add some other content, or use a different template: %>

<% content_for :navigation do %>
  <li><%= link_to 'Login', :action => 'login' %></li>
<% end %>

If you used...

<ul><%= content_for :navigation %></ul>

It would output...

<ul>
  <li><a href="/">Home</a></li>
  <li><a href="/login">Login</a></li>
</ul>

Just tested this locally on a rails 3.1.0 app to make sure this is still the case and it does this fine.

like image 143
lloydpick Avatar answered Oct 18 '22 18:10

lloydpick