问题描述
我已经实现了本文中概述的框架:会遇到一些困难.这可以在预先填充适当的主题和Ajax搜索的情况下起作用,但是当我输入新标签时,如果文本区域失去焦点,则会立即将其删除.我不确定自己在做什么错.这是我的一些相关代码:
I've implemented the framework outlined in this post: How to use jquery-Tokeninput and Acts-as-taggable-on with some difficulty. This is working insofar as prepopulating with the appropriate theme and ajax search, but when I enter a new tag, it is immediately deleted when the text area loses focus. I'm not sure what I'm doing wrong. Here's some of my relevant code:
用户模型(进行标记):
User Model (does the tagging):
class User < ActiveRecord::Base
[...]
# tagging
acts_as_tagger
项目模型(接受标签):
Item Model (accepts a tag):
class Item < ActiveRecord::Base
attr_accessible :title, :tag_list
#tagging functionality
acts_as_taggable_on :tags
项目控制器:
def tags
@tags = ActsAsTaggableOn::Tag.where("tags.name LIKE ?", "%#{params[:q]}%")
respond_to do |format|
format.json { render :json => @tags.collect{|t| {:id => t.name, :name => t.name }}}
end
end
在我的部分表单上:
<%= f.input :tag_list, :label => "Tags", :input_html => { :class => "text_field short", "data-pre" => @item.tags.map(&:attributes).to_json }, :hint => "separate tags by a space" %>
我的路线:
get "items/tags" => "items#tags", :as => :tags
resources :items
[快到了!!!!]
[almost there!!!]
表单上的js [注意:元素的id是动态分配的]:
the js on the form [note: the id of the element is assigned dynamically]:
<script type="text/javascript">
$(function() {
$("#item_tag_list").tokenInput("/art_items/tags", {
prePopulate: $("#item_tag_list").data("pre"),
preventDuplicates: true,
crossDomain: false,
theme: "facebook"
});
});
</script>
推荐答案
如果您仍然想使用Jquery TokenInput并添加标签,则有不同的方法.
If you still want to use Jquery TokenInput and add tags there are different ways to do it.
1.这实际上是我的同一个问题.最新答案:如何使用jquery-Tokeninput和Acts -as-taggable-on
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. 看看这个人的方法: https://github.com/vdepizzol/jquery-tokeninput
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.不确定这一点,但可能会有所帮助: Rails :使用jquery tokeninput(railscast#258)创建新条目
3.Not to sure about this one but it may help: Rails : Using jquery tokeninput (railscast #258) to create new entries
4.这似乎也是合法的: https://github.com/loopj/jquery-tokeninput/pull/219
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.
这篇关于使用jQuery tokeninput和acts_as_taggable_on的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!