grautur
grautur

Reputation: 30485

Using partials on arrays?

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?

Upvotes: 0

Views: 2032

Answers (2)

Chirantan
Chirantan

Reputation: 15634

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.

Upvotes: 3

Jed Schneider
Jed Schneider

Reputation: 14671

I think what you want is:

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

and inside the partial

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

Upvotes: 2

Related Questions