This is a short tutorial about how to use the acts-as-taggable-on gem. Were you about to start coding that tagging feature? Me too. I wanted to add tags like “Rails”, “MongoDB” or “HTML5″ to project listings. That’s no easy task when you start thinking about all the things you want to do with those tags — filtering, searching, managing duplicates, etc. I’m so glad I came across this gem. It saved me hours of time.
Here’s How I Got Started
Add the following to your Gemfile and run bundle install:
gem 'acts-as-taggable-on'
Now generate the migration for the necessary models that will support the tagging and run rake:
rails generate acts_as_taggable_on:migration rake db:migrate
Modify the model where you want to implement tagging:
class Project < ActiveRecord::Base ... attr_accessible :tag_list acts_as_taggable ... end
In your view add an input field for the form where you want to add tags, my example in HAML:
= f.label :tag_list = f.text_field :tag_list, { :placeholder => "Tags separated by commas" }
Now you can retrieve your list of tags:
@project.tag_list.each do |tag| #do something with the tag puts "Tag is #{tag}" end
How did you implement tagging?
Follow @sherm8n
