If you want to add tags, categories, filters, and so on, to your Rails 5 models, the acts-as-taggable-on gem is the way to go. Using it with Rails 5 is fairly straightforward.
1. Add it to your Gemfile:
gem 'acts-as-taggable-on', '~> 6.0'
3. Install its migrations:
acts_as_taggable_on to your model. You don’t need to create a migration for your tags – acts-as-taggable-on handles that for you. You can change
:tags to anything you’d like –
class Post < ApplicationRecord
6. Then, inside your controller, you’ll need to accept
:code_list, etc.) as a strong param. It’s the singular version of the word you used in your model, appended with
class PostsController < ApplicationController
params.require(:post).permit(:title, :body, :tag_list)
7. Now you can add the tags to a form. Sometimes, you may be able to get away with
<%= f.input :tag_list %>, but I’ve had to use a slightly more complicated version in the past. Users can add multiple tags by separating them with commas:
<!-- views/posts/_form.html.erb -->
<%= form.label :tag_list, "Tags" %>
placeholder: 'rails, development'
8. You can search by tagged filters, as well:
get '/tagged', to: "posts#tagged", as: :tagged
@posts = Post.tagged_with(params[:tag])
@posts = Post.all
9. And then list each post, plus the option to sort by tags in your view:
<!-- views/posts/index.html.erb -->
<% @posts.each do |p| %>
<h2><%= p.title %></h2>
<% p.tag_list.each do |t| %>
<%= link_to t, tagged_path(tag: t) %>
<% end %>
<% end %>
Once you understand how it works, acts-as-taggable-on is a pretty easy gem to use. It has a lot of other features, as well, that you can learn more about on its GitHub page. Have fun tagging!