[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it

Target

ezgif.com-video-to-gif.gif

Development environment

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

Premise

The following has been implemented.

Slim introductionIntroduction of Bootstrap3 -Login function implementationImplementation of posting function

Implementation of tag function

1. Introduce Gem

Gemfile


gem 'acts-as-taggable-on'

Gemfile


$ bundle

2. Add table

Terminal


$ rails acts_as_taggable_on_engine:install:migrations

Terminal


$ 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. Edit the model

book.rb


#Postscript
acts_as_taggable

4. Edit the controller

Add tag_list to the strong parameter.

books_controller.rb


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

4. Edit the view

slim:books/new.html.slim


/Postscript
= f.label :tag_list, 'tag'
br
= f.text_field :tag_list, value: @book.tag_list.join(','), id: 'tags'
br

tag_list.join(',') ➡︎ You can add multiple tags by separating them with commas (,).

slim:books/index.html.slim


/Postscript
td
  - book.tag_list.each do |tag|
    span.label.label-default style='margin-right: 10px;'
      = tag

Implementation of input completion function

1. Download and move the required files

** ① Access the link below ** tag-it GitHub

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

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

** ④ Move file **

  1. Unzip the downloaded file.

  2. Place jquery.tagit.css and tagit.ui-zendesk.css in the css folder under ʻassets / stylesheets`.

  3. Place tag-it.js in the js folder under ʻassets / javascripts`. スクリーンショット 2020-06-15 20.48.41.png

2. Introduce Gem

Gemfile


gem 'jquery-ui-rails'

Terminal


$ bundle

3. Edit ʻapplication.js`

application.js


//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require jquery
//= require jquery-ui //Postscript
//= require bootstrap-sprockets
//= require tag-it
//= require_tree .

4. Edit ʻapplication.scss`

application.scss


/*
 *= require_tree .
 *= require jquery.tagit //Postscript
 *= require tagit.ui-zendesk //Postscript
 *= require_self
*/

Recommended Posts

[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
Create a filtering function using acts-as-taggable-on
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
Implementation of user authentication function using devise (1)
Implementation of user authentication function using devise (3)
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of like function
Android application implementation TIPS, reference site summary (function implementation)
Implementation of pagination function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
[Ruby on rails] Implementation of like function
[Rails] Implementation procedure of the function to tag posts without gem + the function to narrow down and display posts by tags
Combination of search and each_with_index
Advantages and disadvantages of Java
Default implementation of Object.equals () and Object.hashCode ()
This and that of exclusive control
iOS app development: Timer app (5. Implementation of alarm and vibration)
Combination of search and each_with_index
[Rails] Implementation of like function
Implementation of tri-tree in Java
Implementation of HashMap in kotlin
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of like function
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
How to implement image posting using rails
[Ruby on rails] Implementation of like function
[Rails] Implementation of validation that maintains uniqueness
Try using the query attribute of Ruby on Rails
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Implementation of like function in Java