jdoe
jdoe

Reputation: 31

Rails partial's local variable doesn't get set

I'm confused with passing a controller's instance variable to a partial template (named after this instance variable).

Documentation from http://api.rubyonrails.org/classes/ActionView/Partials.html says:

By default PartialRenderer uses the template name for the local name of the object passed into the template. These examples are effectively the same:

<%= render :partial => "contract", :locals => { :contract  => @contract } %>
<%= render :partial => "contract" %>

But in my case I can't get the same magic.

ProductsController#show:

@foo = "123456789"

show.html.erb in the following edition works (controller's @foo appears as local variable foo in the _foo.html.erb):

<%= render :partial => 'foo', :locals => { :foo  => @foo } %>

But next code doesn't pass the controller's @foo variable to the _foo.html.erb partial:

<%= render :partial => 'foo' %>

Why so?

Upvotes: 1

Views: 802

Answers (1)

sscirrus
sscirrus

Reputation: 56709

As far as I am aware, locals usually have to be passed explicitly to the partial. The only case in which it may be passed automatically is when you're passing the main object for that controller action, i.e. if you are passing the record @foo in some action for the foos controller.

In your specific case, passing @product should work automatically. If you want to pass @foo, you'll need to pass it explicitly.

Upvotes: 1

Related Questions