I tried to follow the example used in the this article: http://bloginius.com/blog/2013/12/31/how-integrate-acts-as-taggable-on-with-jquery-token-input-with-rails-3/ and applied it to my scenario but in Rails 4.2. as follows:
<%= simple_form_for([:admin, @case_law]) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :citations %>
<%= f.input :case_number %>
<%= f.input :facts, input_html: {rows: 15} %>
<%= f.input :holding, input_html: {rows: 15} %>
<%= f.input :jurisdiction, as: :country, priority: countries %>
<%= f.input :court, collection: court_listings %>
<%= f.input :judgment_date, as: :date_picker %>
<%= f.input :tag_list_tokens, data: {load: @case_law_tags} %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
case_laws.js.coffee
jQuery ->
$('#case_law_tag_list_tokens').tokenInput '/admin/case_laws/tags.json',
theme: 'facebook'
minChars: 2
allowCustomEntry: true
preventDuplicates: true
prePopulate: $('#case_law_tag_list_tokens').data('load')
lib/extensions/tag_extend.rb
module TagExtend
extend ActiveSupport::Concern
included do
scope :by_tag_name, -> name { where("name like ?", "%#{name}%") }
def self.token_input_tags
where(nil).map{ |t| {id: t.name, name: t.name} }
end
end
end
/initializers/acts_as_taggable_on.rb
ActsAsTaggableOn::Tag.send(:include, Intejus::TagExtend)
controllers/admin/case_laws_controller.rb
class Admin::CaseLawsController < AdminController
before_action :find_tags, only: [:new, :create, :edit, :update]
....
def tags
tags = ActsAsTaggableOn::Tag.by_tag_name(params[:q]).token_input_tags
respond_to do |format|
format.json { render json: tags }
end
end
private
def find_tags
@case_law_tags = params[:id].present? ? CaseLaw.find(params[:id]).tags.token_input_tags : []
end
Here is the generated html: <\div class="form-group string optional case_law_tag_list_tokens"><\label class="string optional control-label" for="case_law_tag_list_tokens">Tag list tokens<\ul class="token-input-list-facebook"><\li class="token-input-input-token-facebook"><\input type="text" autocomplete="off" autocapitalize="off" id="token-input-case_law_tag_list_tokens" style="outline: none; width: 209.993048667908px;"><\tester style="position: absolute; top: -9999px; left: -9999px; width: auto; font-size: 11.9999990463257px; font-family: Verdana, sans-serif; font-weight: 400; letter-spacing: 0px; white-space: nowrap;"><\input class="string optional form-control" type="text" name="case_law[tag_list_tokens]" id="case_law_tag_list_tokens" style="display: none;">
My First Issue: If i click the edit page link the token field does not load and a regular text field is shown. If i refresh the edit page the token field appears.
Second Issue: The autocomplete works and the tags are created but the token field does not become prepopulated.
#case_law_tag_list_tokens
element? – ClaytonprePopulate
option,jquery.tokeninput
will look fordata-pre
by default. – Clayton.tokenInput()
in<model>.js
and it's working fine. My hunch is that your.js.coffee
file isn't being precompiled or isn't being included in the page load. What happens if you move that code into a<script></script>
element in your view? – Clayton