Rem*_*mco 10 haml ruby-on-rails ruby-on-rails-3
我有这个each循环:(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
Run Code Online (Sandbox Code Playgroud)
因为@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
Run Code Online (Sandbox Code Playgroud)
但这会产生region_city_attachment_path不正确的结果.第一个每个循环a变量存储正确的值,但我怎么能在第二个循环中reach的第一个a变量each?
ibl*_*lue 16
请给它另一个名字.
- @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
Run Code Online (Sandbox Code Playgroud)
ari*_*uod 13
你在使用变量名时应该更清楚,做类似的事情
- @deals.each do |deal|
.slide
%a{:href => "#"}
- deal.attachments.each do |attachment|
..
Run Code Online (Sandbox Code Playgroud)
当你可以编写一个更易读的代码时,使用诸如"a"/"b"/"x"之类的名称是一个非常糟糕的做法