Finding related tags using acts-as-taggable-on

Posted by user284194 on Stack Overflow See other posts from Stack Overflow or by user284194
Published on 2010-03-18T01:55:58Z Indexed on 2010/03/18 2:01 UTC
Read the original article Hit count: 379

In tag#show I list all entries with that tag. At the bottom of the page I'd like to have something like: "Related Tags: linked, list, of, related tags" My view looks like:

<h2><%= link_to 'Tag', tags_path %>: <%= @tag.name.titleize %></h2>

<% @entries.each do |entry| %>
  <h2><%= link_to h(entry.name), entry %></h2>

  <%- unless entry.phone.empty? -%>
    <p><%= h(entry.phone) %></p>
  <%- end -%>

  <%- unless entry.address.empty? -%> 
    <p><%= h(entry.address) %></p>
  <%- end -%>

  <%- unless entry.description.empty? -%>
    <p><%= h(entry.description) %></p>
  <%- end -%>

  <p2><%= link_to "more...", entry %><p2>
<% end %>

Related Tags: 
<% @related.each do |tag| %>
  <%= link_to h(tag.tags), tag %>
<% end %>

tags_controller.rb:

def show
  @title = Tag.find(params[:id]).name
  @tag = Tag.find(params[:id])
  @entries = Entry.paginate(Entry.find_tagged_with(@tag), 
            :page => params[:page], :per_page => 10, :order => "name")
  @related = Entry.tagged_with(@tag, :on => :tags)
end

Every entry has at least one tag, it's required by the entry model. I'd like duplicate tags ignored and the current tag (the tag that the list belongs to) ignored. My current code displays this:

Related Tags: Gardens Gardens ToursGardens

Gardens is a link to the entry, not the tag gardens. ToursGardens is a link to the entry that includes those tags.

My desired result would be:

Related Tags: Gardens, Tours

Each link would link to it's associated tag. Can anyone help me achieve this? I tried using a div_for but I don't think that was right.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby