Open Source ProjectsCode that Might be Useful to You

Talks I've GivenOn Technologies and Ideas

ThoughtsWhere I Sometimes Write Things

Resume If You Still Believe In Those

Follow Me On

GitHubIf coding is your thing

TwitterIf you tweet

TumblrIf you're ADD

Adding a tag listing

My next move was to be able to have a page listing all the known tags in the system. I first refactored the tags action to by_tag, so that it reads a bit better, and so I could use tags action for my tag list action.

Here’s what the new tags action looks like:

def tags
  @tags = BlogPost.tag_counts

This tags_counts is actually added to your model when you add acts_as_taggable to your model class. The name is a bit misleading, but it does provide you a list of tags for the given model. The Tag object also includes a count of how many things are using it, hence the name tag_counts. The acts_as_taggable homepage suggests this was intended to help you generate a tag cloud.

Here’s what my rhtml looks like:

<h2>All known tags:</h2>
  <% @tags.each do |tag| %>
    <li><%= link_to, {:controller => 'blog', :action => 'by_tag', :tag =>} %></li>
  <% end %>

And with that, I have a some nice tag listing action going on. In a future iteration, I hope to turn this into a tag cloud.

comments powered by Disqus