LoginSignup
2
4

More than 3 years have passed since last update.

【Rails】acts-as-taggable-onを用いたタグ機能と、tag-itを用いたタグ入力補完機能の実装

Posted at

目標

ezgif.com-video-to-gif.gif

開発環境

・Ruby: 2.5.7
・Rails: 5.2.4
・Vagrant: 2.2.7
・VirtualBox: 6.1
・OS: macOS Catalina

前提

下記実装済み。

Slim導入
Bootstrap3導入
ログイン機能実装
投稿機能実装

タグ機能の実装

1.Gemを導入

Gemfile
gem 'acts-as-taggable-on'
Gemfile
$ bundle

2.テーブルを追加

ターミナル
$ rails acts_as_taggable_on_engine:install:migrations
ターミナル
$ rails db:migrate
schema.rb
create_table "taggings", force: :cascade do |t|
  t.integer "tag_id"
  t.string "taggable_type"
  t.integer "taggable_id"
  t.string "tagger_type"
  t.integer "tagger_id"
  t.string "context", limit: 128
  t.datetime "created_at"
  t.index ["context"], name: "index_taggings_on_context"
  t.index ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
  t.index ["tag_id"], name: "index_taggings_on_tag_id"
  t.index ["taggable_id", "taggable_type", "context"], name: "taggings_taggable_context_idx"
  t.index ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy"
  t.index ["taggable_id"], name: "index_taggings_on_taggable_id"
  t.index ["taggable_type"], name: "index_taggings_on_taggable_type"
  t.index ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type"
  t.index ["tagger_id"], name: "index_taggings_on_tagger_id"
end

create_table "tags", force: :cascade do |t|
  t.string "name"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer "taggings_count", default: 0
  t.index ["name"], name: "index_tags_on_name", unique: true
end

3.モデルを編集

book.rb
# 追記
acts_as_taggable

4.コントローラーを編集

ストロングパラメーターにtag_listを追加する。

books_controller.rb
def book_params
  params.require(:book).permit(:title, :body, :tag_list)
end

4.ビューを編集

books/new.html.slim
/ 追記
= f.label :tag_list, 'タグ'
br
= f.text_field :tag_list, value: @book.tag_list.join(','), id: 'tags'
br

tag_list.join(',')
➡︎ カンマ(,)区切りにする事で複数のタグを追加する事が出来る。

books/index.html.slim
/ 追記
td
  - book.tag_list.each do |tag|
    span.label.label-default style='margin-right: 10px;'
      = tag

入力補完機能の実装

1.必要ファイルをダウンロードし、移動

①下記リンクにアクセス
tag-it GitHub

Clone or downloadをクリック
スクリーンショット 2020-06-15 20.41.58.png

Download ZIPをクリック
スクリーンショット 2020-06-15 20.42.03.png

④ファイルを移動

1.ダウンロードしたファイルを解凍する。

2.cssフォルダ内のjquery.tagit.csstagit.ui-zendesk.cssassets/stylesheets配下へ配置する。

3.jsフォルダ内のtag-it.jsassets/javascripts配下へ配置する。
スクリーンショット 2020-06-15 20.48.41.png

2.Gemを導入

Gemfile
gem 'jquery-ui-rails'
ターミナル
$ bundle

3.application.jsを編集

application.js
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require jquery
//= require jquery-ui // 追記
//= require bootstrap-sprockets
//= require tag-it
//= require_tree .

4.application.scssを編集

application.scss
/*
 *= require_tree .
 *= require jquery.tagit // 追記
 *= require tagit.ui-zendesk // 追記
 *= require_self
*/
2
4
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
2
4