gem installation
gem 'acts-as-taggable-on', '~> 6.0'
bundle install
Create table
$ rake acts_as_taggable_on_engine:install:migrations
$ rake db:migrate
Save tag Add the following to the model you want to tag. (This time I wanted to tag the Post model, so write it in post.rb) From the model folder
class Post < ApplicationRecord
acts_as_taggable
end
Add the following with registration edit
<div class="field">
<%= f.label :tag_list %><br />
<%= f.text_field :tag_list, value: @user.tag_list.join(","), class:"form-control" %>
</div>
If you want to separate them with spaces, create acts_as_taggable_on.rb in config / initializers / and write as follows.
ActsAsTaggableOn.delimiter = ' '
users show.html.erb should also be below
<% @user.tag_list.each do |tag| %>
<span class="badge badge-primary mr-1 p-2"><%= tag %></span>
<% end %>
Or
<% @post.tag_list.each do |tag| %>
<span style="background-color: #e9e9e9; border-radius: 5px;"><%= tag %></span>
<% end %>
Do you have the following for application.controller?
devise_parameter_sanitizer.permit(:account_update, keys:[:tag_list])
Additional Information Obviously, there are various methods available.
find_related_skills Tasks related to the same tag are displayed Although it is not the taggings_count method, the number of times the corresponding tag has been used is displayed. tag_counts All tag data can be acquired And so on ..... there were more.
Please refer to the reference for how to use it.
If the tags are registered with the same name, they will have the same ID. Example: Even if you register with Rails and a separate task, the id will be the same
Reference URL https://qiita.com/ddgg7755/items/e1caa8b73d118822a0a2 https://qiita.com/daigou26/items/e67e61bbec338329ffac https://qiita.com/kazuhj/items/31d3b1751d9c957b789f
If you want to use Tag-it
# Gemfile
gem "jquery-ui-rails"
bundle install
Add the following to vendor / assets
https://github.com/aehlke/tag-it
curl https://raw.githubusercontent.com/aehlke/tag-it/master/js/tag-it.js -o vendor/assets/javascripts/tag-it.js
curl https://raw.githubusercontent.com/aehlke/tag-it/master/css/jquery.tagit.css -o vendor/assets/stylesheets/jquery.tagit.css
curl https://raw.githubusercontent.com/aehlke/tag-it/master/css/tagit.ui-zendesk.css -o vendor/assets/stylesheets/tagit.ui-zendesk.css
application.js
//= require jquery
//= require rails-ujs
//= require jquery-ui
//= require tag-it
//= require activestorage
//= require turbolinks
//= require_tree .
app/assets/stylesheets/application.css *= require_tree . *= require jquery-ui *= require jquery.tagit *= require tagit.ui-zendesk *= require_self
Probably needed
gem 'jquery-rails', '4.3.1'
edit.html
<!-- #taggable #tag-Added by it-->
<div class="field",id: "user_tag_list>
<%= f.label :list%>
<%= f.text_field :tag_list, value: @user.tag_list.join(","), class:"form-control" %>
</div>
<%= javascript_tag do %>
var myProject = {
all_tag_list: <%= raw @all_tag_list %>
};
<% end %>
js
$(document).on 'ready page:load', ->
$('#article_tag_list').tagit
singleField: true,
availableTags: myProject.all_tag_list
See autocomplete or below https://qiita.com/tyamagu2/items/75eeaa8ef208385aa341 http://rochefort.hatenablog.com/entry/2016/10/16/165139
Recommended Posts