简体   繁体   中英

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) %>,&nbsp;
<% end %>

There must be an elegant way.

<%= safe_join(@regions.map { |region| link_to(region.name, region_path(region)) }, ', ') %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM