Return to Snippet

Revision: 20707
at November 23, 2009 15:19 by smoothdzion


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

Revision: 20706
at November 23, 2009 14:11 by smoothdzion


Updated Code
# Counts Categories
<% category_count = @post.categories.length %>
# Creates an index option
<% @post.categories.each_with_index do |c, index| %>
  # Adds a value to the index option
  <% order = index+1 %>
  # Creates a link and adds a comma and space if the order doesn't equal the total index count.
  <%= link_to c.name, c %><%= ", " unless category_count == order %>
<% end %>

Revision: 20705
at November 23, 2009 14:08 by smoothdzion


Updated Code
<% category_count = @post.categories.length %>
<% @post.categories.each_with_index do |c, index| %>
  <% order = index+1 %>
  <%= link_to c.name, c %><%= ", " unless category_count == order %>
<% end %>

Revision: 20704
at November 23, 2009 14:06 by smoothdzion


Initial Code
<% category_count = @post.categories.length %>
    <% @post.categories.each_with_index do |c, index| %>
      <% order = index+1 %>
      <%= link_to c.name, c %><%= ", " unless category_count == order %>
    <% end %>

Initial URL

                                

Initial Description
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.

Initial Title
RoR - Category Link with Comma Separation

Initial Tags
rails, ruby

Initial Language
Rails