Let Ruby on Rails ActiveRecord has many useful tags.
How to use my plugin.
Add this line to your application's Gemfile:
gem 'usefultag'
And then execute:
bundle
rails usefultag:install:migrations
rails db:migrate
private
def book_params
...
book_tags: []
...
end
has_many_useful_tags :book_tags
# Mode methods
book_tags_str('-')
#=> tag1-tag2-tag3
book_tags_str
#=> tag1,tag2,tag3
<%= form_with(model: book, local: true) do |form| %>
...
<div class="field">
<%= form.label :book_tags %>
<%= form.useful_tags_field :book_tags %>
</div>
...
<% end %>
This will generate a input, all available tags will pop up when foucs on the input.
Just click on those added tags for remove them.
Remember to permit params in Controller
class Comment < ApplicationRecord
belongs_to :post
has_many_useful_tags :tags, scope: :post_id
end
This can set every post have diffrent tags on comments.
<%= form_with(model: comment, local: true) do |form| %>
...
<div class="field">
<%= form.label :tags %>
<%= form.useful_tags_field :tags, scope: comment.post_id %>
</div>
...
<% end %>
Mount usefultag to main app route.
Rails.application.routes.draw do
mount Usefultag::Engine => "/usefultag"
end
The gem is available as open source under the terms of the MIT License.