每个循环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” /“×”,当你可以写出更多可读代码