Posted By

smoothdzion on 11/23/09


Tagged

rails ruby views


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

smoothdzion


RoR - Category Link with Comma Separation


 / Published in: Rails
 

For separating a list of categories with a comma that are linked. There may be a better way. Leave a comment if you have a better way. This was the first way I found that worked.

This code increments the count 1 at a time. It applies the increment to the next loop of data.

  1. <% category_count = @post.categories.length %><% count = 1 %>
  2. <% @post.categories.each do |cat| %>
  3. <%= link_to cat.name, cat %><%= ", " unless category_count == count %><% count += 1 %>
  4. <% end %>

Report this snippet  

You need to login to post a comment.