1
votes

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.

1
Can you post a sample of the output of the HTML for the form? Or at least the #case_law_tag_list_tokens element?Clayton
@Clayton should the data-load attribute be generated in the input tag?DaneEH
Minor simplicity tweak: if you don't specify a prePopulate option, jquery.tokeninput will look for data-pre by default.Clayton
I don't know enough about Coffeescript or the rails asset pipeline to diagnose this, but I know that in my app I just put the call to .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
@daveomcd i have an attr_reader with def tag_list_tokens=(tokens) self.tag_list = tokens.gsub("'","") endDaneEH

1 Answers

1
votes

I realised that i should have used a text field: <%= f.text_field :tag_list_tokens, data: {load: @case_law_tags} %> instead of the simple_form helper <%= f.input :tag_list_tokens, data: {load @case_law_tags} %>

The problem the reloading was related to turbolinks. Rails does not do a complete reload of the document when following a link. It loads the differences between pages through ajax. I fixed the issue by modifying my code as:

case_laws.js.coffee

tokenizeCaseLawTags = ->
  $('#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')

$(document).on "ready page:load", ->
  tokenizeCaseLawTags()