views:

42

answers:

1

I have an index view that lists all of the tags for my Entry and Message models. I would like to only show the tags for Entries in this view. I'm using acts-as-taggable-on.

Tags Controller:

def index
  @letter = params[:letter].blank? ? 'a' : params[:letter]
  @tagged_entries = Tagging.find_all_by_taggable_type('Entry').map(&:taggable)
  @title = "Tags"
  if params[:letter] == '#'
    @data = Tag.find(@tagged_entries, :conditions => ["name REGEXP ?",
      "^[^a-z]"], :order => 'name', :select => "id, name")
  else
    @data = Tag.find(@tagged_entries, :conditions => ["name LIKE ?",
            "#{params[:letter]}%"], :order => 'name', :select => "id, name")
  end
  respond_to do |format|
    flash[:notice] = 'We are currently in Beta. You may experience errors.'
    format.html
  end
end

tags#index:

<% @data.each do |t| %>
  <div class="tag"><%= link_to t.name.titleize, tag_path(t) %></div>
<% end %>

I want to show only the taggable type 'Entry' in the view. Any ideas? Thank you for reading my question.

SECOND EDIT:

Tags Controller:

def index
  @title = "Tags"
  @letter = params[:letter].blank? ? 'a' : params[:letter]

  @taggings = Tagging.find_all_by_taggable_type('Entry', :include => [:tag, :taggable])
  @tags = @taggings.map(&:tag).sort_by(&:name).uniq
  @tagged_entries = @taggings.map(&:taggable)#.sort_by(&:id)#or whatever

  if params[:letter] == '#'
    @data = Tag.find(@tags, :conditions => ["name REGEXP ?",
        "^[^a-z]"], :order => 'name', :select => "id, name")
  else
    @data = Tag.find(@tags, :conditions => ["name LIKE ?",
              "#{params[:letter]}%"], :order => 'name', :select => "id, name")
  end

  respond_to do |format|
    format.html
  end
end

tags#index:

<% @data.each do |t| %>
  <div class="tag"><%= link_to t.name.titleize, tag_path(t) %></div>
<% end %>

Max Williams' code works except when I click on my alphabetical pagination links. The error I'm getting [after I clicked on the G link of the alphabetical pagination] reads:

Couldn't find all Tags with IDs (77,130,115,...) AND (name LIKE 'G%') (found 9 results, but was looking for 129)

Can anyone point me in the right direction?

+1  A: 
  @taggings = Tagging.find_all_by_taggable_type('Entry', :include => [:tag, :taggable])
  @tags = @taggings.map(&:tag).sort_by(&:name)
  @tagged_entries = @taggings.map(&:taggable).sort_by(&:id)#or whatever
Max Williams
For the most part this works. I explained the exact behavior in detail above. Thank you so much for your help!