开发者

Using partials on arrays?

开发者 https://www.devze.com 2023-01-26 16:15 出处:网络
I\'m confused by how partials behave with respect to arrays. Simple Example I have the following in a view:

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 ar开发者_如何学运维ray?


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.


I think what you want is:

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

and inside the partial

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

精彩评论

暂无评论...
验证码 换一张
取 消