・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina
The following has been implemented.
・ Slim introduction ・ Introduction of Bootstrap3 -Login function implementation ・ Implementation of posting function
Gemfile
gem 'acts-as-taggable-on'
Gemfile
$ bundle
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
book.rb
#Postscript
acts_as_taggable
Add tag_list
to the strong parameter.
books_controller.rb
def book_params
params.require(:book).permit(:title, :body, :tag_list)
end
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
** ① Access the link below ** tag-it GitHub
** ② Click Clone or download
**
** ③ Click Download ZIP
**
** ④ Move file **
Unzip the downloaded file.
Place jquery.tagit.css
and tagit.ui-zendesk.css
in the css
folder under ʻassets / stylesheets`.
Place tag-it.js
in the js
folder under ʻassets / javascripts`.
Gemfile
gem 'jquery-ui-rails'
Terminal
$ bundle
application.js
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require jquery
//= require jquery-ui //Postscript
//= require bootstrap-sprockets
//= require tag-it
//= require_tree .
application.scss
/*
*= require_tree .
*= require jquery.tagit //Postscript
*= require tagit.ui-zendesk //Postscript
*= require_self
*/
Recommended Posts