Dont have a comma on the last iteration of an each loop in Rails
if you want to do minimum possible change to your code, you can use the following
<%= topics.each do |topic| %>
<a href="<%= topic.link %>" ><%= topic.name %></a>
<% if(topic != topics.last) %>
,
<% end %>
<% end %>
One way of doing this is with map
then Array#join
:
<%= topics.map { |topic| link_to(topic.name, topic.link) }.join(',').html_safe %>
How about using each_with_index
, and only put comma before the content unless it's not the first item.
<% topics.each_with_index do |topic, i| %>
<% if i > 0 %>
,
<% end %>
<a href="<%= topic.link %>"><%= topic.name %></a>
<% end %>