Richlewis
Richlewis

Reputation: 15384

Print Array as comma separated list in a .each block

If i have this simple block

<% p.sectors.each do |s| %>
  <%= s.name %>
<% end %>

That iterates through all of the sectors , how would i get the results to print out in a comma separated sentence, so something like this

web design, web development, Software testing

There is a method called to_sentence but is that relevant here and how do i apply this method?

Thanks

Upvotes: 1

Views: 492

Answers (2)

chuang wang
chuang wang

Reputation: 65

<%= p.sectors.map{|item| item.name}.join(',')%>

Upvotes: 1

Thaha kp
Thaha kp

Reputation: 3709

<%= p.sectors.map(&:name).join(", ") %>

Upvotes: 4

Related Questions