Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to follow along with the instructions on how to implement jquery-tokeninput here:

How to use jquery-Tokeninput and Acts-as-taggable-on

I am trying to attach tags to a "post". I can use the text field in my form to create new tags. However, the javascript just will not fire at all. It's as if none of the code even exists. I don't get any error messages. The text field just acts as a normal text field with no javascript events firing. What can be the issue? Here is my relevant code:

post model

attr_accessible :tag_list
acts_as_taggable_on :tags

posts controller

def tags 
    query = params[:q]
    if query[-1,1] == " "
      query = query.gsub(" ", "")
      Tag.find_or_create_by_name(query)
    end

    #Do the search in memory for better performance

    @tags = ActsAsTaggableOn::Tag.all
    @tags = @tags.select { |v| v.name =~ /#{query}/i }
    respond_to do |format|
      format.json{ render :json => @tags.map(&:attributes) }
    end
  end

routes # It has to find the tags.json or in my case /products/tags.json get "posts/tags" => "posts#tags", :as => :tags

application.js

$(function() {
  $("#post_tags").tokenInput("/posts/tags.json", {
    prePopulate:       $("#post_tags").data("pre"),
    preventDuplicates: true,
    noResultsText:     "No results, needs to be created.",
    animateDropdown:   false
  });
});

Out of frustration I also created a posts.js.coffee file just to see if that was the problem:

$ ->
  $("#post_tags").tokenInput "/posts/tags.json",
    prePopulate: $("#post_tags").data("pre")
    preventDuplicates: true
    noResultsText: "No results, needs to be created."
    animateDropdown: false

post form view

<%= f.label :tag_list %>
<%= f.text_field :tag_list, :id => "post_tags", "data-pre" => @post.tags.map(&:attributes).to_json %>

Am I missing something here? Any help would be greatly appreciated!!!

Thanks!!

share|improve this question
 
Have you checked to make sure that application.js is getting loaded on the site? –  Justin Wood Dec 17 at 2:17
 
$(function() { alert(123); }); Does this work in your application.js? –  beck03076 Dec 17 at 11:43
add comment

This question has an open bounty worth +50 reputation from Observer ending in 3 hours.

This question has not received enough attention.

1 Answer

i am not sure what you have or not but make sure that you are using the gem "jquery-rails" and that in your layouts/file you include

<%= stylesheet_link_tag "application", "token-input-facebook" %>
<%= javascript_include_tag :defaults, "jquery.tokeninput" %>

in your controller.rb file you will need to have

def index
  @model = Model.where("name like ?", "%#{params[:q]}%")
  respond_to do |format|
    format.html
    format.json { render :json => @model.map(&:attributes) }
  end
end

Take a look at http://railscasts.com/episodes/258-token-fields and http://loopj.com/jquery-tokeninput/ they really helped me put to implement the same thing

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.