2012-07-29 92 views
8

我有此each循環:(HAML)每個循環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 

由於@deals被組合3個表(模型)我使用polymorphic_path以生成所述圖像的鏈接的查詢。

- @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 

但是這產生了region_city_attachment_path這是不正確的。第一個循環a變量存儲正確的值,但我怎麼能reach第一個a變量在第二個each循環?

回答

13

只是給它另一個名字。

- @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 
1

只是不要使用他們兩個相同的名稱,一切都會很好。

13

使用變量名時,你應該更清楚,這樣做

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

這是一個非常不好的做法,使用的名稱,如「A」 /「B」 /「×」,當你可以寫出更多可讀代碼