views:

358

answers:

1

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.

+1  A: 
@related_entries = Entry.tagged_with(@tag, :on => :tags)
@related_tags = @related_entries.collect{|x|x.tags}.flatten.uniq
Apie
Using your code in my tags controller and: <% @related_tags.each do |tag| %> <%= link_to tag.name, tag %> <% end %>in my view it almost works perfectly. The only problem is that it shows the current tag with the other tags. How do I remove @tag from @related_tags?Thank you for your help, I appreciate it.
array.delete(element) will remove an element from an array so if you call @related_tags.delete(@tag) it will remove the tag from the array. Good luck.
Apie
Thanks Apie, you've been a great help.