Forum: Ruby on Rails newbie confused by div_for

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
502c9355791371cccf14f43014925850?d=identicon&s=25 Alex Chekholko (chalex)
on 2009-03-18 23:52
Hi,

Absolute Rails beginner here.

in app/views/players/show.html.rb:

<%= render :partial => @player.items %>

in app/views/items/_item.html.erb

<ul>
<% div_for item do %>
<li>
<%= h(item.name) %>
</li>
<% end %>
</ul>


That works fine.  I'm trying to get rid of the partial, so I do
something like:

in app/views/players/show.html.rb

<ul>
<% div_for @player.items do %>
<li>
<%= h(item.name) %>
</li>
<% end %>
</ul>

That doesn't work with error "undefined local variable or method `item'"

How do I iterate over the items?

From script/console:
>> player.items
=> [#<Item id: 4, name: "Chopsticks", quantity: nil, player_id: 2,
created_at: "2009-03-18 16:39:42", updated_at: "2009-03-18 16:39:42">,
#<Item id: 5, name: "Bowl", quantity: nil, player_id: 2, created_at:
"2009-03-18 17:04:12", updated_at: "2009-03-18 17:04:12">]
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2009-03-18 23:59
(Received via mailing list)
On Mar 18, 2009, at 3:52 PM, Alex Chekholko wrote:

>
> something like:
>
> That doesn't work with error "undefined local variable or method
> `item'"
>
> How do I iterate over the items?

<ul>
<% @player.items.each do |item| %>
   <% div_for item do %>
     <li><%= h item.name %></li>
   <% end %>
<% end %>
</ul>
502c9355791371cccf14f43014925850?d=identicon&s=25 Alex Chekholko (chalex)
on 2009-03-19 00:33
Philip Hallstrom wrote:
> On Mar 18, 2009, at 3:52 PM, Alex Chekholko wrote:
>
>>
>> That doesn't work with error "undefined local variable or method
>> `item'"
>>
>> How do I iterate over the items?
>
> <ul>
> <% @player.items.each do |item| %>
>    <% div_for item do %>
>      <li><%= h item.name %></li>
>    <% end %>
> <% end %>
> </ul>

Thanks, I get it now!
This topic is locked and can not be replied to.