Ruby-on-rails 在模板中循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5496678/
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
loop in templates
提问by demas
My template looks like:
我的模板看起来像:
<h2>Oracle</h2>
<% @q_oracle.each do |q| %>
<%= link_to(q.title + ' (' + q.answer_count.to_s + ') ' + q.question_id.to_s, 'http://stackoverflow.com/' + q.question_answers_url) %> </br>
<% end %>
<h2>Ruby and Rails</h2>
<% @q_ruby.each do |q| %>
<%= link_to(q.title + ' (' + q.answer_count.to_s + ') ' + q.question_id.to_s, 'http://stackoverflow.com/' + q.question_answers_url) %> </br>
<% end %>
So the temlate consists the static titles (h2) and loops through the array. I am searching the way to avoid copy-paste code in the my template. Something like:
因此模板包含静态标题 (h2) 并循环遍历数组。我正在寻找避免在我的模板中复制粘贴代码的方法。就像是:
@hash = { 'Oracle' => @q_oracle, 'Ruby and Rails' => @q_ruby }
@hash.each { |@t, @a|
<h2>@t</h2>
<% @a.each do |q| %>
<%= link_to(q.title + ' (' + q.answer_count.to_s + ') ' + q.question_id.to_s, 'http://stackoverflow.com/' + q.question_answers_url) %> </br>
<% end %>
}
Is it possible?
是否可以?
回答by Viacheslav Molokov
Yes you can do it.
是的,你可以做到。
Ruby 1.9 solution
Ruby 1.9 解决方案
In Ruby 1.8 this solution can be used when titles order doesn't matter. In Ruby 1.9 titles would appear in order they were inserted in hash.
在 Ruby 1.8 中,当标题顺序无关紧要时,可以使用此解决方案。在 Ruby 1.9 中,标题会按照它们插入哈希的顺序出现。
Just place this variable to your controller action:
只需将此变量放置到您的控制器操作中即可:
@hash = { 'Oracle' => @q_oracle, 'Ruby and Rails' => @q_ruby }
And access this variable from your view:
并从您的视图访问此变量:
<% @hash.each do |t, a| %>
<h2><%= t %></h2>
<% a.each do |q| %>
<%= link_to(q.title + ' (' + q.answer_count.to_s + ') ' + q.question_id.to_s, 'http://stackoverflow.com/' + q.question_answers_url) %> </br>
<% end %>
<% end %>
Ruby 1.8 with sorted keys
带有排序键的 Ruby 1.8
This method sorts keys so they appear in alphabetical order.
此方法对键进行排序,使它们按字母顺序出现。
<% @hash.keys.sort.each do |t| %>
<h2><%= t %></h2>
<% @hash[t].each do |q| %>
<%= link_to(q.title + ' (' + q.answer_count.to_s + ') ' + q.question_id.to_s, 'http://stackoverflow.com/' + q.question_answers_url) %> </br>
<% end %>
<% end %>
Ruby 1.8 with arrays
带有数组的 Ruby 1.8
This method would behave like Ruby 1.9 in any ruby version - titles would appear in order they were added.
此方法在任何 ruby 版本中的行为都类似于 Ruby 1.9 - 标题将按添加顺序出现。
Variable @hashmust be initialized as:
变量@hash必须初始化为:
@hash = [ ['Oracle', @q_oracle], ['Ruby and Rails',@q_ruby] ]
View must be updated to:
视图必须更新为:
<% @hash.each do |title_with_questions| %>
<h2><%= title_with_questions[0] %></h2>
<% title_with_questions[1].each do |q| %>
<%= link_to(q.title + ' (' + q.answer_count.to_s + ') ' + q.question_id.to_s, 'http://stackoverflow.com/' + q.question_answers_url) %> </br>
<% end %>
<% end %>
回答by Steve Jorgensen
It's certainly possible, but it's putting an awful lot of Ruby logic in a view, which is kind of smelly. I think it would be better to factor out duplicated the question-list body portion into a partial.
这当然是可能的,但它在视图中放入了大量的 Ruby 逻辑,这有点令人讨厌。我认为最好将重复的问题列表正文部分分解为部分内容。
Main view...
主视图...
<h2>Oracle</h2>
<%= render :partial => 'question_list', :locals => {:questions => @q_oracle} %>
<h2>Ruby and Rails</h2>
<%= render :partial => 'question_list', :locals => {:questions => @q_ruby} %>
Partial: _question_list.html.erb ...
部分:_question_list.html.erb ...
<% questions.each do |q| %>
<%= link_to(q.title + ' (' + q.answer_count.to_s + ') ' + q.question_id.to_s, 'http://stackoverflow.com/' + q.question_answers_url) %> </br>
<% end %>
This approach is more readable than the nested loop, and it's also more flexible in terms of customizing the page structure. For instance: what if you want to apply different styling to each list, or place a divider in between?
这种方法比嵌套循环更具可读性,并且在自定义页面结构方面也更灵活。例如:如果你想对每个列表应用不同的样式,或者在它们之间放置一个分隔符怎么办?
回答by Jeffrey W.
But of course it's possible to loop through an Hash.
但是当然可以循环遍历Hash.
<% @hash.each do |title, var|
<h2><%= title %></h2>
... so forth ...
<% end %>

