I have written my first Rails gem, called trivial_tokens!

This gem handles some of the boilerplate of a typical jQuery tokenInput implementation in rails. It adds a getter and setter for the tokenised form of a model’s assocations, a query callback to the association’s controller, and provides a generator for the typical minimum javascript required.

The gem makes it really easy to implement tokenInput in rails, it adds:

A class method ‘tokenize’ on ActiveRecord models, which takes the name of a relation on the model as argument. This method adds getter/setter methods for the ‘tokenized’ form of the relation - ie, a comma delimited string of IDs.

A controller around_action :index callback to provide handling for the ‘q’ parameter that tokenInput expects.

A generator to provide the default javascript to create a tokenInput field for the tokenized attribute on forms.

Jquery tokeninput assets (CSS & JS)

Using the gem

Trivial Tokens assumes you wish to provide a token input for a rails association of either has_many, or has_and_belongs_to_many type, so ensure that this is set up.

class Article < ActiveRecord::Base
  has_and_belongs_to_many :tags
  #...
end

To add the model methods trivial_tokens requires: add a call to tokenize, passing the association name:

class Article < ActiveRecord::Base
  has_and_belongs_to_many :tags
  tokenize :tags
  #...
end

In the model’s controller, permit the tokenised_tags parameter:

class Article < ActiveRecord::Base
  #...
  def article_params
    params.require(:article).permit(:tokenized_tags, :other_params)
  end
end

In the association’s controller, add a call to tokenize:

class TagsController < ApplicationController
  tokenize
  #...
end

Next, in application.js, require tokenInput:

//= require jquery.tokeninput

And in application.css:

*= require token-input

or

*= require token-input-facebook

or

*= require token-input-mac

depending on your needs.

To generate the minimum javascript required for the jquery tokenInput functionality, run the token_input generator. This generator takes the model and its association as arguments:

$ rails generate trivial_tokens:token_input article tag

This will generate a javascript file in app/assets/javascripts which will call tokenInput on fields with id:

#article_tokenized_tags

So go ahead and add a tokenized_tags field to your form view:

<!-- ... -->
<div class="field">
  <%= f.label :tokenized_tags %>
  <%= f.text_field :tokenized_tags %>
</div>
<!-- ... -->

that’s it!