Coderama
Coderama

Reputation: 11362

Weird problem: collection object displaying in view?

When I run the following code in the view:

- if @object.winner.present? && @object.winner.prizes.any?
  %ul
    = @object.winner.prizes.each do |p|
      %li= p.description.html_safe

It's returning this:

li prize1
li prize2
li prize3
...
[#<Prize object...> ... ]

Does anyone know why it is listing the @object.winner.prizes collection in the view directly after the last @object.winner.prizes object is displayed?

Extremely baffled! I'm using Rails v3.0.6

Upvotes: 0

Views: 115

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107738

This is because you're using = when you should be using - to iterate over the objects:

- @object.winner.prizes.each do |p|

Upvotes: 1

Related Questions