如何在Rails中使用jquery tokeninput? [英] How to use jquery tokeninput in Rails?

查看:65
本文介绍了如何在Rails中使用jquery tokeninput?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用中的表单中添加一个tokeninput jquery字段,允许用户发布状态更新。我希望用户能够将作品(单独的模型)附加到状态更新。我正在使用act_as_taggable_on gem,我的查询指定标签上下文工作。但是,该字段不会加载任何搜索结果。

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.

我实际上有第二个tokeninput字段,允许用户将标记附加到状态更新,就像这个网站使用标签一样附加到此发行票。它工作正常!我正在尝试镜像该功能以指定搜索工作模型的上下文,我正在努力实现。

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:

发布模型

attr_accessible :content, :tag_list, :work_list

acts_as_taggable_on :tags
acts_as_taggable_on :works

帖子控制器(更新)

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

表格

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

javascript

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

路线

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

谢谢!

编辑:我清理了这个问题并更新了所有代码。我还开始在enginhere.com上进行对话,其中还有其他工程师的其他疑难解答:

I cleaned up this question and the updated all of the code. I also started a conversation on enginhere.com which also has some other troubleshooting from other engineers:

http://bit.ly/179kiqH

通过继续上述enginhere.com对话的对话,随时提供帮助在这里发布最终的,正式答案为赏金!

Feel free to help by continuing the conversation on the above enginhere.com conversation and then posting the final, official answer here for the bounty!

再次感谢!

推荐答案

本主题对任何问题的精彩帖子回答!

Amazing post answers to any questions by this topic!

http://bloginius.com/blog/2013/12/31/how -integrate-acts-as-taggable-on-with-jquery-token-input-with-rails-3 /

这篇关于如何在Rails中使用jquery tokeninput?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆