Acts As Taggable On Gem: How-To and Use Cases

Allene Norton
4 min readJan 4, 2021
acts-as-taggable-on setup from https://github.com/mbleigh/acts-as-taggable-on

I recently adapted an open-source Rails application to connect volunteers to volunteer opportunities for a non-profit contract. The app uses the gem “acts-as-taggable-on”, and I had to learn how to configure it for our purposes, and I even ended up adding further functionality to the app using the gem, so I figured I would write about my experiences and give a brief description of how it works.

Setup

Initial setup is pretty easy! After adding the gem to your gemfile and running ‘bundle install’, you need to install the migrations that come packaged with the gem by running the command:

‘rake acts_as_taggable_on_engine:install:migrations’

Then, run a normal ‘rake db:migrate’. That’s it! After this, you are ready to start setting it up for use.

Use

First, you’ll want to set up in your model the tags you wish to make available. They can be anything you want! Keep in mind, tags follow typical Rails naming conventions when it comes to pluralization. You can find more info about those here.

Next, you want to permit the tag lists in your controller params related to the model you are using the tags for. Following the naming conventions, if you have a tag context called ‘skills’ in your model, you will want to permit the ‘skill_list’ when defining your strong params.

example params from https://github.com/mbleigh/acts-as-taggable-on

That is more or less all you need to do to start adding tags. The full use instructions and documentation can be found here. You find that the gem allows you to add and remove tags from a list and search your database for instances tagged with your query.

The example above doesn’t quite demonstrate what I needed to do to add the tag lists to my strong params, so here is an example for my skills and categories tag lists:

example params from my project

Thinking Outside-The-Box

My schema for volunteer projects includes a column for project location. However, I wanted to implement a quicker and easier way to filter projects by multiple parameters: skills, project types, locations, and categories. Even though I have the location available in my database, I found that adding the locations tag context to my project and the project creation form and then using the tag to set the location in the database got me much faster and cleaner results when filtering.

To set this up, I set the form options for location to radio buttons. This way, even though the project has a ‘:location_list’ param, the location will always be the first and only item in the location list. In order to access the tag lists using a form, you do need to set ‘multiple’ to ‘true’, but using the radio button ensures that the user can only select one option when creating a project location. The use of tags in forms is not clearly explained in the gem documentation. You can also allow users to set multiple tags if you use checkboxes instead of radio buttons. Below is an example of my form view that circumvents allowing for multiple selections:

example of using tag list in form

After setting that up, I changed the create method in my controller to look like this:

example create method

Now, I can use the gem to tag a project with a location that is stored both in the appropriate database column and the tag itself! Allowing multiple tag contexts on my project model lets me easily add and remove access to specific data about a project without necessarily having to write new migrations and add columns. I can definitely see this being useful for a variety of applications, and I hope this post illuminates a bit more about the functionality of this useful gem!

--

--

Allene Norton

Full stack developer and Flatiron graduate who recently made the jump from a career as a professional musician and audio engineer | Austin, TX