Wednesday, 15 February 2012

ruby - Dont have a comma on the last iteration of an each loop in Rails -



ruby - Dont have a comma on the last iteration of an each loop in Rails -

i want print out list of links separated commas in rails.

heres i've got:

<%= topics.each |topic| %> <a href="<%= topic.link %>" ><%= topic.name %></a> , <% end %>

heres want:

<a href="thing_a">thing a</a>, <a href="thing_b">thing b</a>, <a href="thing_c">thing c</a>

but right comma on lastly iteration of loop! should do?

one way of doing map array#join:

<%= topics.map { |topic| link_to(topic.name, topic.link) }.join(',').html_safe %>

ruby-on-rails ruby

No comments:

Post a Comment