每个循环Haml?

每个循环Haml?

问题描述:

我有此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循环?

只是给它另一个名字。

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

只是不要使用他们两个相同的名称,一切都会很好。

使用变量名时,你应该更清楚,这样做

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

这是一个非常不好的做法,使用的名称,如“A” /“B” /“×”,当你可以写出更多可读代码