HAML each loop with array

851 views
Skip to first unread message

William Reithmeyer

unread,
Feb 4, 2016, 4:07:34 PM2/4/16
to Haml
- array = [[:name => "Hogan's Heros", :url => "http://www.gstatic.com/tv/thumb/tvbanners/184204/p184204_b_v8_ac.jpg" ], [:name => "Get Smart", :url => "http://www.gstatic.com/tv/thumb/movieposters/176339/p176339_p_v8_af.jpg" ], [:name => "Hawaii Five-0", :url => "http://www.gstatic.com/tv/thumb/tvbanners/8130405/p8130405_b_v8_aj.jpg" ], [:name => "The Big Lebowski", url => "http://t3.gstatic.com/images?q=tbn:ANd9GcRBYp315X-0pNvI-Dvqj8FR0AGdF39VCprXpurd0cQel__e17CP" ]]
-array.each do |movie|
    .img{ :style => "background-image: url('#{movie['url']}');" }
    .name #{movie['name']}

I looked everywhere online, can't find anything.
Why am i getting an error for having this array and the each loop?

> HAML compilation of haml/index.html.haml failed!
> [#] Error: undefined local variable or method `url' for #<Object:0x000000049b6140>
even when I change it to :url instead of 'url' I get the same problem.

engineerDave

unread,
Feb 4, 2016, 5:04:31 PM2/4/16
to Haml
you're trying to assign hashes { :key => value} in array syntax 

[[:name=> "text"]] vs [{:name => "text"}]

try to instantiate your array variable in an irb console and you'll see the issue. 

Matt Wildig

unread,
Feb 4, 2016, 5:13:52 PM2/4/16
to ha...@googlegroups.com
The error isn’t in the Haml, it’s in your Ruby where you create the array:
should be

:url => "http://...

The “url” here:

> .img{ :style => "background-image: url('#{movie['url']}');" }

is just a string and doesn’t affect anything.


Matt

Reply all
Reply to author
Forward
0 new messages