Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Each loop Haml?

I have this each loop: (haml)

- @deals.each do |a|
     .slide
        %a{:href => "#"}
         - a.attachments.each do |a|
           = image_tag(a.file.url, :height =>"325px", :width =>"650px" )
            .caption{:style => "bottom:0"} 
              = a.description

Because @deals is combined query of 3 tables (models) I use polymorphic_path to generate the links of the images.

- @deals.each do |a|
     .slide
        %a{:href => "#"}
         - a.attachments.each do |a|
           = image_tag(a.file.url, :height =>"325px", :width =>"650px" ), polymorphic_path(@region, @city, a)
            .caption{:style => "bottom:0"} 
              = a.description

But this generates region_city_attachment_path which is not correct. The first each loop a variable store the correct value, but how can I reach the first a variable in the second each loop?

like image 689
Remco Avatar asked Jul 29 '12 22:07

Remco


3 Answers

Just give it another name.

- @deals.each do |a|
     .slide
        %a{:href => "#"}
         - a.attachments.each do |b|
           = image_tag(a.file.url, :height =>"325px", :width =>"650px" ), polymorphic_path(@region, @city, b)
            .caption{:style => "bottom:0"} 
              = a.description
like image 183
iblue Avatar answered Nov 07 '22 12:11

iblue


you should be more clear when using variable names, do something like

- @deals.each do |deal|
  .slide
    %a{:href => "#"}
      - deal.attachments.each do |attachment|
        ..

it's a really bad practice to use names such as "a"/"b"/"x" when you can write a much more readable code

like image 43
arieljuod Avatar answered Nov 07 '22 12:11

arieljuod


Just don't use the same name for both of them, and everything will turn out fine.

like image 25
Ry- Avatar answered Nov 07 '22 10:11

Ry-