Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render @object and locals vs render :partial

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?

like image 843
SteenhouwerD Avatar asked Apr 03 '12 09:04

SteenhouwerD


People also ask

What is render partial 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 can you tell Rails to render without a layout?

2.2. By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.

What is Local_assigns?

local_assigns is a Rails view helper method that you can check whether this partial has been provided with local variables or not. Here you render a partial with some values, the headline and person will become accessible with predefined value.

What is render in Ruby on Rails?

Rendering is the ultimate goal of your Ruby on Rails application. You render a view, usually . html. erb files, which contain a mix of HMTL & Ruby code. A view is what the user sees.


1 Answers

<%= 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.

like image 101
Berggeit Avatar answered Sep 22 '22 15:09

Berggeit