Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 878
Ximik Avatar asked Feb 27 '12 19:02

Ximik


People also ask

What is the difference between Content_for and yield?

They are opposite ends of the rendering process, with yield specifying where content goes, and content_for specifying what the actual content is.

What is the reason for having partials in Rails?

Rails Guides describes partials this way: Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.

How do you render a partial?

You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .


1 Answers

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.

like image 174
Baldrick Avatar answered Sep 27 '22 16:09

Baldrick