Render @object and locals vs render: partial

I want to pass a local variable that contains the origin on a specific page, this variable contains only a character with a value.

When I use this code, it works fine, the origin variable is available in partial:

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

But when I use this code, the source is not set and is not available in partial:

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

What is the difference? The second line of code does not display partial, how is the first line?

+7
ruby-on-rails-3 renderpartial partials
Apr 03 2018-12-12T00:
source share
2 answers
 <%= render @products %> 

Indeed, shorthand syntax for partial mapping. But with shorthand syntax, Rails ignores the ": locals" variable. More on this in the Rails Guides .

So, if you want to pass additional parameters to rendering, you must specify ": ​​partial => ...". If you want to know why this happens, you can take a look at the source of Rails.

+8
Apr 18 2018-12-12T00:
source share

There is a good explanation here: Rails: confuse syntax for passing local users to partial

The short option is that you can simply omit :locals in the second example:

 render @products, :origin => :gallery 
+1
Mar 29 '13 at 1:19
source share



All Articles