13

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?

user229044
  • 232,980
  • 40
  • 330
  • 338
Ximik
  • 2,435
  • 3
  • 27
  • 53

2 Answers2

12

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.

Baldrick
  • 23,882
  • 6
  • 74
  • 79
  • Yes, I understood this. But how can I make rails use another boot sequence? Or maybe there is another solution. I want to have the opportunity to render more than one peaces from my partial. – Ximik Feb 27 '12 at 20:08
  • @Ximik Maybe you can put `stuff` in its own partial, so it can be called both by the layout and by `user/bar.html.erb` – Baldrick Feb 28 '12 at 10:33
  • It really seems to be the best solution in this case. Thank you. – Ximik Feb 28 '12 at 18:31
5

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 %>
PJSCopeland
  • 2,818
  • 1
  • 26
  • 40