xjq233p_1
xjq233p_1

Reputation: 8070

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?

Upvotes: 8

Views: 2844

Answers (1)

lloydpick
lloydpick

Reputation: 1659

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.

Upvotes: 8

Related Questions