6

I'm trying to add a tokeninput jquery field in a form in my app that allows users to post status updates. I want users to be able to attach works (a separate model) to the status update. I'm using the act_as_taggable_on gem and my query specifies the tags context "works". However, the field will not load any search results.

I actually have a second tokeninput field that allows users to attach tags to the status update, much like this website uses tags to attach to this issue ticket. It works fine! I'm trying to mirror that functionality to specify the context to search the works model and I'm struggling with the implementation.

Any ideas? Your time and assistance would be greatly appreciated! Here is the relevant code:

post model

attr_accessible :content, :tag_list, :work_list

acts_as_taggable_on :tags
acts_as_taggable_on :works

 

Post controller (updated)

def work_list
 query = params[:q]  
 @work_list = ActsAsTaggableOn::Tag.includes(:taggings).where("taggings.context = 'works'").where("tags.name ILIKE ?", "%#{params[:q].downcase.to_s}%").all
 @work_list = @work_list.select { |v| v.name =~ /#{query}/i }
  respond_to do |format|
    format.json { render :json => @work_list.map{|w| {:id => w.name, :name => w.name }}}
  end
end
    

def tags 
    query = params[:q]
    if query[-1,1] == " "
      query = query.gsub(" ", "")
      ActsAsTaggableOn::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{|t| {:id => t.name, :name => t.name }}}
    end
  end

form

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

javascript

$ ->
  $("#post_tags").tokenInput "/posts/tags.json",
    prePopulate: $("#post_tags").data("pre")
    preventDuplicates: true
    noResultsText: "No results, press space key to create a new tag."
    animateDropdown: false

$ ->
  $("#post_work_list").tokenInput "/posts/work_list.json",
    prePopulate: $("#post_work_list").data("pre")
    preventDuplicates: true
    noResultsText: "No results"
    animateDropdown: false

routes

get "posts/tags" => "posts#tags", :as => :tags
get "posts/work_list" => "posts#work_list", :as => :work_list
0

1 Answer 1

2

If you are using acts_as_taggable_on gem it's easier way than in railscast.

Model (Post):

acts_as_taggable_on :works

View (form):

= f.text_field :work_list, "data-pre" => f.object.work_list.sort.collect {|t| {id: t, name: t } }.to_json

JS:

$ ->
  $("#post_work_list").tokenInput "/posts/works.json",
    preventDuplicates: true,
    animateDropdown: false
Sign up to request clarification or add additional context in comments.

1 Comment

I updated the code in the question. Is there other code I can add on this page for you? It's a private repo so I can't give read only access. Are there other alternatives?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.