Newbie confused by div_for


#1

Hi,

Absolute Rails beginner here.

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

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

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

    <% div_for item do %>
  • <%= h(item.name) %>
  • <% end %>

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

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

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

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”>]


#2

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?

    <% @player.items.each do |item| %> <% div_for item do %>
  • <%= h item.name %>
  • <% end %> <% end %>

#3

Philip H. 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?

    <% @player.items.each do |item| %> <% div_for item do %>
  • <%= h item.name %>
  • <% end %> <% end %>

Thanks, I get it now!