Ruby-on-rails 每个循环Haml?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11713367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Each loop Haml?
提问by Remco
I have this eachloop: (haml)
我有这个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
Because @dealsis combined query of 3 tables (models) I use polymorphic_pathto generate the links of the images.
因为@deals是我polymorphic_path用来生成图像链接的3 个表(模型)的组合查询。
- @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_pathwhich is not correct. The first each loop avariable store the correct value, but how can I reachthe first avariable in the second eachloop?
但这会产生region_city_attachment_path不正确的结果。第一个每个循环a变量存储正确的值,但是如何在第二个循环中存储reach第一个a变量each?
回答by iblue
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
回答by arieljuod
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
当您可以编写更具可读性的代码时,使用诸如“a”/“b”/“x”之类的名称是一种非常糟糕的做法
回答by Ry-
Just don't use the same name for both of them, and everything will turn out fine.
只是不要对它们使用相同的名称,一切都会好起来的。

