Question

I'm confused by how partials behave with respect to arrays.

Simple Example

I have the following in a view:

render :partial => "foobars", :object => [1, 2, 3]

And in _foobars.html.erb, I have

<%= foobars.size %>
<%= foobars[0] %>

The weird thing is that what gets displayed is "444" and "101", not "3" and "1". Is something special happening because I'm passing in an array?

Was it helpful?

Solution

I think what you want is:

render :partial => "foobars", :locals => {:object => [1, 2, 3]}

and inside the partial

<%= object.size %>
<%= object[0] %>

OTHER TIPS

What Jed says works but what you are looking for is really

render :partial => "foobars", :collection => [1,2,3]

Inside the partial, the iteration will happen by itself on the passed array and foobars will hold the array element of each iteration

<%= foobars %>

will give 1, 2 and 3 inside the partial.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top