8

I want to pass a local variable that contains the origin to come on a specific page, this variable contains just a symbol with the value.

When I use this code it works perfect, the origin variable is accessible in the partial :

render :partial => "products", :collection => @products, :locals => {:origin => :gallery}

But when I use this code, the origin is not set and not accessible in the partial :

render @products, :locals => {:origin => :gallery}

What is the difference here? Is the second line of code not render the partial like the first line?

SteenhouwerD
  • 1,819
  • 1
  • 16
  • 22

2 Answers2

9
<%= render @products %>

Is indeed the shorthand syntax for rendering a partial. But with the shorthand syntax, Rails will ignore the ":locals" variable. There's more on this in the Rails Guides.

So if you want to pass extra options to the render, you have to specify ":partial => ...". If you want to know why this happens, you can take a look at the Rails source.

Berggeit
  • 253
  • 1
  • 2
  • 11
1

There's a good explanation here: Rails: confused about syntax for passing locals to partials

The short version is that you can just omit :locals in the second example:

render @products, :origin => :gallery
Community
  • 1
  • 1
gmcnaughton
  • 2,233
  • 1
  • 21
  • 28
  • Definitely doesn't work for me (Rails 3.2.13). The link to the other question is helpful. But I think your example is wrong because you're trying to use the collection shortcut, which unfortunately doesn't let you pass locals. You're probably thinking of `render "product", :origin => :gallery` – Arcolye Apr 02 '13 at 13:39