tohach
tohach

Reputation: 63

cannot add new tags through jquery.tokeninput

I have watched numerous times both ryan rails-casts, however I'm stuck on this issue couple of days. The main problem is I cannot set new tag records with jquery.tokeninput nor without jquery. I'm pretty sure it has something to do with wrong routes setup... Thank you in advance!

#prepopulating form works fine

_form.html.haml

= f.text_field :tag_list, data: {load: @job.tags.map(&:attributes).to_json }

job.js.coffee

jQuery ->
  $('#job_tag_list').tokenInput '/jobs/tags.json',
  theme: 'facebook'
  prePopulate: $('#job_tag_list').data('load')

job.rb

attr_accessible :tag_list


def self.tokens(query)

  tags = ActsAsTaggableOn::Tag.all.where("name LIKE ?", "%#{query}%")
  if tags.empty?
    [{id: "<<<#{query}>>>", name: "Add new skill:  \"#{query}\""}]
  else
    tags
  end
end


def self.tag_list=(arguments)
  return if !arguments.is_a?(Hash)
  list = arguments[:tag_list]
  list.gsub!(/<<<(.+?)>>>/) { ActsAsTaggableOn::Tag.find_or_create_by_name(name: $1).name }
end

jobs_controller.rb

def tags

  @tags  = ActsAsTaggableOn::Tag.where("tags.name LIKE ?", "%#{params[:q]}%")
  respond_to do |format|
    format.json {render :json => @tags.tokens(params[:q])}
  end
end

routes.rb

    resources :jobs do
    member do
      post 'reply'
    end
  end

Upvotes: 0

Views: 478

Answers (2)

cristi_razvi
cristi_razvi

Reputation: 764

$("#skills").tokenInput("/url",{
        theme: "facebook",
        onResult: function (results) {
            if ( results.length == 0 ){
              result = new Object();
              result['id']    = $('#token-input-skills').val();
              result['name']  = $('#token-input-skills').val();
              results.push(result);
            }
            return results;
        }
      });

i added a function that populates the array with the user's input text if no results are available

Upvotes: 0

tohach
tohach

Reputation: 63

the workable integration of act_as_taggable_one and jquery.tokeninput is provided below:

Extract files from here:https://github.com/loopj/jquery-tokeninput and put in corresponding folders:

vendor/assets/javascripts *vendor/assets/stylesheets*

gemfile

gem 'acts-as-taggable-on'

application.css

 *= require token-input-facebook

application.js

//= require jquery.tokeninput

application.html.erb

<%= javascript_include_tag :defaults, "jquery.tokeninput" %>

jobs.js.coffee

jQuery ->
  $('#job_tag_list').tokenInput '/jobs/tags.json',
    theme: 'facebook'
    prePopulate: $('#job_tag_list').data('load')
    allowCustomEntry: true

_form.html.haml

= f.text_field :tag_list, data: {load: @job.tags.map{|t| {id: t.name, name: t.name}}.to_json}

jobs_controller.rb

def tags
    @tags  = Job.tokens(params[:q])
    respond_to do |format|
      format.json {render :json => @tags}
    end
  end

job.rb

acts_as_taggable
attr_accessible :tag_list

def self.tokens(query)
    tags = ActsAsTaggableOn::Tag.where("tags.name LIKE ?", "%#{query}%")
    if tags.empty?
      [{id: "#{query}", name: "Add new skill:  \"#{query}\""}]
    else
      tags
    end
  end

routes.rb

 resources :jobs do
    member do
      post 'reply'
    end
      get 'tags', on: :collection
  end

Upvotes: 1

Related Questions