10 December 2016
Last week I worked on an issue where I had to add category tags to a particular model in our backend. While attempting a manual implementation, I found there was a gem that did this.
When it came to configuring the delimiter characters, the README on the repository wasn't very clear.
With the default implementation of the gem, the behaviour is as follows:
This behaviour is apparently by design.
Configure acts_as_taggable_on to use both spaces and commas to separate tags. And make sure commas are not stripped when editing.
Following the gem's README:
# Gemfile
gem 'acts-as-taggable-on'
and in the terminal, bundle:
bundle install
run migrations
rake acts_as_taggable_on_engine:install:migrations
rake db:migrate
#app/models/trip.rb
class Trip
acts_as_taggable
end
Add :tag_list
to the whitelist
#app/controllers/trips_controller.rb
def create
@trip = Trip.create(trip_params)
redirect_to @trip
end
private
def trip_params
params.require(:trip).permit(:tag_list)
end
# config/routes.rb
resources :tags, only:[:index, :show]
Add the delimiters to the Application_controller
#app/controllers/application_controller.rb
ActsAsTaggableOn.delimiter=[',',' ']
Then in the form, the syntax for the input should be:
#app/views/trips/_form.html.haml
= f.input :tag_list, label: "Tags", input_html: { value: @trip.tag_list.to_s }
#app/views/trips/show.html.slim
h2 Tags
= render @trip.tags
That is all.