Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render partial :collection => @array specify variable name

I am rendering a partial like this:

$("#box_container").html("<%= escape_javascript( render :partial => 'contacts/contact_tile', :collection => @contacts) %>") 

Problem is that my partial is expecting the variable 'contact'.

ActionView::Template::Error (undefined local variable or method `contact' 

I simply want to tell the partial to expect a variable contact. Should iterate through @contacts as contact. How do I do that?

like image 315
botbot Avatar asked Aug 30 '12 00:08

botbot


People also ask

How do you render a partial?

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

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.

How can you tell Rails to render without a layout *?

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.


1 Answers

Found this is also helpful from the docs. You aren't limited to having the variable named after the partial:

http://guides.rubyonrails.org/layouts_and_rendering.html

To use a custom local variable name within the partial, specify the :as option in the call to the partial:

<%= render :partial => "product", :collection => @products, :as => :item %> 

With this change, you can access an instance of the @products collection as the item local variable within the partial."

like image 74
botbot Avatar answered Oct 01 '22 17:10

botbot