如何使用jQuery-Tokeninput和行为-AS-taggable上 [英] How to use jquery-Tokeninput and Acts-as-taggable-on
问题描述
这是你如何使用自动完成与 jQuery的Tokeninput 并的 ActsAsTaggableOn 。
This is how you use autocomplete with jQuery Tokeninput and ActsAsTaggableOn.
在我的情况我使用的是嵌套形式,但它不应该没关系。下面的一切都是code的作品。
In my situation i am using a nested form but it shouldnt matter. Everything below is code that works.
产品型号:
attr_accessible :tag_list # i am using the regular :tag_list
acts_as_taggable_on :tags # Tagging products
产品控制器:
#1. Define the tags path
#2. Searches ActsAsTaggable::Tag Model look for :name in the created table.
#3. it finds the tags.json path and whats on my form.
#4. it is detecting the attribute which is :name for your tags.
def tags
@tags = ActsAsTaggableOn::Tag.where("tags.name LIKE ?", "%#{params[:q]}%")
respond_to do |format|
format.json { render :json => @tags.map{|t| {:id => t.name, :name => t.name }}}
end
end
路线:
# It has to find the tags.json or in my case /products/tags.json
get "products/tags" => "products#tags", :as => :tags
application.js中:
Application.js:
$(function() {
$("#product_tags").tokenInput("/products/tags.json", {
prePopulate: $("#product_tags").data("pre"),
preventDuplicates: true,
noResultsText: "No results, needs to be created.",
animateDropdown: false
});
});
形式:
<%= p.text_field :tag_list,
:id => "product_tags",
"data-pre" => @product.tags.map(&:attributes).to_json %>
第1期(解决)
必须具有行:
Issue 1(SOLVED)
Must have the line:
format.json { render :json => @tags.collect{|t| {:id => t.name, :name => t.name }}}
请注意 - 您可以使用 @ tags.map
在这里也和你没有任何改变的形式
Note - You can use @tags.map
here as well and you dont have to change the form either.
下面是关于为什么你需要做的这2个问题:
Below are the 2 issues on why you needed to do this:
我有如下的标签
: {ID:1,名:民以食为天}
。当我保存产品
标记民以食为天
,它应该保存为 ID:1
在搜索时,发现该名民以食为天
。目前,它节省了新的标签
一个新的ID,它引用民以食为天
ID,即 {ID:19日,名:1}
。相反,它应该是找到ID,显示名称,并做了 find_or_create_by
所以它不会创建一个新的标签
。
I have the following Tag
: {"id":1,"name":"Food"}
. When I save a Product
, tagged "Food"
, it should save as ID: 1
when it searches and finds the name "Food"
. Currently, it saves a new Tag
with a new ID that references the "Food"
ID, i.e. {"id":19,"name":"1"}
. Instead, it should be finding the ID, showing the name, and doing a find_or_create_by
so it doesn't create a new Tag
.
当我去产品/节目
通过执行&LT看到标签$ C>。名称显示为标签:1 ,当它真正应该是标签:食品。
When I go to products/show
to see the tags by doing <%= @product.tag_list %>
. The name appears as "Tags: 1", when it really should be "Tags: Food".
我该如何解决这些问题?
How can I fix these issues?
推荐答案
您应该在你的的routes.rb
这应该处理产品定义路由/标签
路径。你可以将其定义如下:
You should define a route in your routes.rb
which should handle products/tags
path. You can define it like:
get "products/tags" => "products#tags", :as => :tags
因此,应该给你一个 tags_path
助手应该评估为 /产品/标签
。这应该摆脱你在问题中提到的错误。请务必资源之前定义添加这条路线:产品
在的routes.rb
Thus should give you a tags_path
helper which should evaluate to /products/tags
. This should get rid of the errors you mentioned in the question. Be sure to add this route before defining resources :product
in your routes.rb
现在到充当-AS-taggable上,我没有使用这种宝石,但你应该看看方法 all_tag_counts
文档。你的的ProductsController#代码
方法需要在以下几行一些变化。我不知道它到底是什么将是必须的,因为我用Mongoid,不能对其进行测试。
Now onto acts-as-taggable-on, I haven't used this gem, but you should look at method all_tag_counts
documentation. Your ProductsController#tags
method will need some changes on the following lines. I am not sure if its exactly what would be required, as I use Mongoid and can't test it out.
def tags
@tags = Product.all_tag_counts.(:conditions => ["#{ActsAsTaggableOn::Tag.table_name}.name LIKE ?", "%#{params[:q]}%"])
respond_to do |format|
format.json { render :json => @tags.collect{|t| {:id => t.name, :name => t.name } }
end
end
这篇关于如何使用jQuery-Tokeninput和行为-AS-taggable上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!