Cookies setting
1

Tagging with acts-as-taggable-on

As you know, tags are one of the key features of web 2.0. Personally I don't really care much for tags, but I had to implement them in a project of mine.
If you need a very basic tagging functionality, this tutorial covers that as well. To save yourself some time you can use the plugin called
acts-as-taggable-on. It basically covers all your tagging needs. I find the documentation on github a bit unclear, so I've decided to write a quick and clear tutorial on how to make this thing work. There are two parts, first I describe the basics and then I'll go in to ma more complex example.

 

The model
You need to add this to the model you want to add:

acts_as_taggable_on :keywords

 

The view
In your form you need to add a text field like this:

= f.text_field :keyword_list


In your show view you need to add this:
  -unless @post.keywords.empty?
    %p
      Tags:
      = @post.keyword_list
This covers the basic tagging of an object.

 

Acts-as-taggable and Sphinx
If you implemented a search with
Sphinx, to index the tags you just need to add this to the model:


  define_index do
    indexes keywords.name, :as => :keywords
  end

 

 

 

The complex part

 

Now for the more complex part (it actually isn't that complex), here is how to tag a bunch of posts in your blog.

You need to create a new page named tagged (or whatever) and set up the routes for it.

map.tagged "tagged", :controller => "posts", :action => "tagged"


The controller
In the posts controller you need to add a new action called tagged.

def tagged
  @tags = Post.tag_counts_on(:keywords)
  @tagged_posts = Post.tagged_with(params[:keyword])
end

This will get all the tags for the tag cloud and the posts attached to a specific tag.

 

The helper
In the post helper you need to add this:


module PostsHelper
 include ActsAsTaggableOn::TagsHelper
end


The view
The form to add the tags is already done, all you need to do now is create a tag cloud and transform the tags into links.
Here is the code for the cloud partial:


    -tag_cloud(@tags, %w(css1 css2 css3 css4 css5 css6 css7) )do |k, c|
      =link_to k.name, {:keyword => k.name, :action => "tagged", :controller => "pages"}, :class => c


Transforming the tags into links is very similar:

-unless post.keywords.empty?
  - post.keywords.each do |k|
     =link_to k.name, {:keyword => k.name, :action => "tagged", :controller => "pages"}

What is left is the tagged.html.haml that will display all the posts tagged with a specific tag.

-@tagged_posts.each do |post|
  = render :partial => 'posts/post', :locals => {:post => post}

This about covers it. I hope this short tutorial explains a little better how tagging with acts-as-taggable-on works. If you have anything to add on the subject, leave a comment below.
 

15 Aug 2010 at 20:42
RoR


Alex Muraro
22 Jul 2012 at 21:57

thanks for this, I have written a new tutorial that builds up from your article, you can find it here, let me know if you spot any error :)

http://www.alexmuraro.me/posts/acts-as-taggable-on-a-short-tutorial



(required)

(required)