使用 jquery tokeninput 和acts_as_taggable_on
我已经实现了这篇文章中概述的框架:如何使用 jquery-Tokeninput 和 Acts-as-taggable-on 有一些困难。就预填充适当的主题和 ajax 搜索而言,这是有效的,但是当我输入新标签时,当文本区域失去焦点时,它会立即被删除。我不确定我做错了什么。这是我的一些相关代码:
用户模型(进行标记):
class User < ActiveRecord::Base
[...]
# tagging
acts_as_tagger
项目模型(接受标签):
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
[几乎在那里!!!]
表单上的js [注意:元素的 id 是动态分配的]:
<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>
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
Item Controller:
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
On my form partial:
<%= 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" %>
my routes:
get "items/tags" => "items#tags", :as => :tags
resources :items
[almost there!!!]
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您仍然想使用 Jquery TokenInput 并添加标签,可以采用不同的方法。
1.
这实际上来自我的同一个问题;最新答案:如何使用 jquery-Tokeninput 和 Acts -as-taggable-on
这可以放在你的控制器中。
2.
看看这个人的方法: https://github.com/vdepizzol/jquery-tokeninput
他自定义输入能力:
3.
不确定这个,但它可能会有所帮助: Rails :使用 jquery tokeninput (railscast #258) 创建新条目
4.
这似乎也是合法的: https://github.com/loopj/jquery-tokeninput /pull/219
我个人喜欢第一个,似乎最容易获取和安装。
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.
2.
Check out this guy's method: https://github.com/vdepizzol/jquery-tokeninput
He made a custom entry ability:
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.