Question

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.

Was it helpful?

Solution

@related_entries = Entry.tagged_with(@tag, :on => :tags)
@related_tags = @related_entries.collect{|x|x.tags}.flatten.uniq
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top