Ximik
Ximik

Reputation: 2495

Rails: Content_for in partial

I have something like this in my layout

...
<%= yield :test %>
...
<%= render :partial => 'user/bar' %>

And in user/bar.html.erb I have

<% content_for :test do %>
stuff
<% end %>

And this doesn't seem to work. And I have found out that yield :test executes before partial, but after executing the view of the action. Why does it do so and what can I do?

Upvotes: 13

Views: 8581

Answers (2)

PJSCopeland
PJSCopeland

Reputation: 3006

I wrote the partial into a local variable before calling yield, and then rendered it into the document later:

...
<% partial = render(:partial => 'user/bar') %>
<%= yield :test %>
...
<%= partial %>

Upvotes: 5

Baldrick
Baldrick

Reputation: 24340

The syntax content_for :test do ... end captures the content of the block, and content_for :test gives the captured block. doc for content_for.

In your code, the restitution is done before the capture, so it cannot work.

Upvotes: 12

Related Questions