[英]Ruby each loop separate by commas
I am trying to get this loop to separate the results by a comma: 我正在尝试使此循环以逗号分隔结果:
<% @regions.each do |region| %>
<%= link_to region.name, region_path(region) %>
<% end %>
If I do this, it adds a comma after the last result: 如果我这样做,它将在最后一个结果之后添加一个逗号:
<% @regions.each do |region| %>
<%= link_to region.name, region_path(region) %>,
<% end %>
There must be an elegant way. 必须有一种优雅的方式。
<%= safe_join(@regions.map { |region| link_to(region.name, region_path(region)) }, ', ') %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.