Each loop Haml?

25,682

Solution 1

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

Solution 2

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

Solution 3

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

Share:
25,682
Remco
Author by

Remco

Updated on July 05, 2022

Comments

  • Remco
    Remco almost 2 years

    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?