Rails beginner
Rails beginner

Reputation: 14504

Rails loop formatting how to put 'and' after the second last word?

I want to but and just after the second loop.

Example I have:

<% kon.tags.each do |d| %>
<%= link_to d.name, tag_path(d.kategori.slug, d.slug) %>,
<% end %>

It outputs: link, link, link, link,

What I want is: link, link, link and link

Upvotes: 0

Views: 93

Answers (1)

pjumble
pjumble

Reputation: 16960

Rails provides a helper for this (Array#to_sentence), so you can do:

<%= raw kon.tags.collect { |d| link_to d.name, tag_path(d.kategori.slug, d.slug) }.to_sentence %>

Although be careful if you're using raw for user input.

Upvotes: 2

Related Questions