Using jquery tokeninput and acts_as_taggable_on

假装没事ソ 提交于 2019-12-03 08:44:17
LearningRoR

If you still want to use Jquery TokenInput and add tags there are different ways to do it.

1. This is actually from my same question; the newest answer: How to use jquery-Tokeninput and Acts-as-taggable-on

This could go in your 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

This will create the tag, whenever the space bar is hit.

You could then add this search setting in the jquery script:

noResultsText: 'No result, hit space to create a new tag',

It's a little dirty but it works for me.

2. Check out this guy's method: https://github.com/vdepizzol/jquery-tokeninput

He made a custom entry ability:

$(function() {
  $("#book_author_tokens").tokenInput("/authors.json", {
    crossDomain: false,
    prePopulate: $("#book_author_tokens").data("pre"),
    theme: "facebook",
    allowCustomEntry: true
  });
});

3. Not to sure about this one but it may help: Rails : Using jquery tokeninput (railscast #258) to create new entries


4. This one seems legit as well: https://github.com/loopj/jquery-tokeninput/pull/219

I personally like the first one, seems easiest to get and install.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!