How to do tagging in Rails with gem ‘Acts_as_taggable_on’

ror41
Social sharing

ror41Tagging is a way to give a keyword and search by that particular keyword. In Rails “acts-as-taggable-on” is one of the popular gem for tagging. With ‘acts_as_taggable_on’, you can tag a single model on several contexts, such as skills, interests, and awards

Below is an example to describe how to implement it in a Rails application:

    Step#1
  • Add the gem to your Gemfile:
gem ‘acts-as-taggable-on'
  • Then run the bundle
  • bundle install

    Step#2

  • Run the generator command
  • rails g acts_as_taggable_on:migration
  • Run the migration file
  • rake db:migrate

    This migration will create two tables in the database: tags and taggings.
    Step#3
    Now you have to modify your model where you want to implement tagging.
    For example, we like to implement the tagging on Post model.

  • Add the following codes to the “Post” model to have acts_as_taggable and also add a “tag_list” field to the attr_accessible list.
  • attr_accessible :content, :name, :tag_list
    acts_as_taggable

    Step#4

  • Add a new field to your post form.
  • <%= f.label :tag_list, "Tags (separated by commas)" %>
    <%= f.text_field :tag_list %>

    Step#5

  • Add the following codes, if you want to show the tags for the tagged posts in the listing page:
  • <% @posts.each do |post| %>
    <h2><%= link_to post.name, post %></h2>
    <%= simple_format post.content %>
    <p>
    Tags: <%= raw post.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %>
    </p>
    <% end %>

    Step#6

  • Modify your route file to get the tags
  • get 'tags/:tag', to: 'posts#index', as: :tag

    Step#7

  • Modify your controller:
  • class PostsController < ApplicationController
    def index
    if params[:tag]
    @posts = Post.tagged_with(params[:tag])
    else
    @posts = Post.all
    end
    end
    end
    

    Voila! You have done it.

    Your recently viewed posts:

    Jayadev Das - Post Author

    Do what you do best in – that’s what I’ve always believed in and that’s what I preach. Over the past 25+ years (yup that’s my expertise ‘n’ experience in the Information Technology domain), I’ve been consulting to small, medium and large companies ‘About Web Technologies, Mobile Future as well as on the good-and-bad of tech. Blogger, International Business Advisor, Web Technology Expert, Sales Guru, Startup Mentor, Insurance Sales Portal Expert & a Tennis Player. And top of all – a complete family man!

      Contact Us

      We’d love to help & work with you




      When do you want to start ?


      Enter your email address to stay up to date with the latest news.
      Holler Box

      Orange Exit pop up

      Subscribe for the latest
      trends in web and
      mobile app development
      Holler Box

      Exit pop up

      Sad to see you leaving early...

      From "Aha" to "Oh shit" we are sharing everything on our journey.
      Enter your email address to stay up to date with the latest news.
      Holler Box