LoginSignup
1
2

More than 3 years have passed since last update.

rails5でacts-as-taggable-onを使う手順

Last updated at Posted at 2018-07-15

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
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2