gemをインストール
Gemfile
gem 'acts-as-taggable-on', :git => 'https://github.com/mbleigh/acts-as-taggable-on'
$ bundle install
タグ用テーブルを作成
$ rake acts_as_taggable_on_engine:install:migrations
$ rails db:migrate
Model
app/models/entry.rb
class Entry < ApplicationRecord
acts_as_taggable_on :job
acts_as_taggable
end
Controller
app/controllers/entries_controller.rb
class EntriesController < ApplicationController
def entry_params
params.require(:entry).permit(:job_list)
end
end
View
edit.html.erb
<%= f.radio_button :job_list %>
<%= f.label :job_list %>
タグの一覧を表示する方法
Model
app/models/model.rb
class Book < ApplicationRecord
acts_as_taggable_on :tag
acts_as_taggable
end
Controller
app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
@tags = Book.all_tags
end
end
共通箇所(サイドナビなど)でタグの一覧を表示する方法
Controller
app/controllers/concerns/common.rb
def list
@taglist = ActsAsTaggableOn::Tag.for_context(:tags)
@categorylist = ActsAsTaggableOn::Tag.for_context(:categories)
end