通过 rails3-jquery-autocomplete 使用多个输入字段

Posted

技术标签:

【中文标题】通过 rails3-jquery-autocomplete 使用多个输入字段【英文标题】:Use multiple input fields with rails3-jquery-autocomplete 【发布时间】:2012-03-02 19:05:46 【问题描述】:

我正在将https://github.com/crowdint/rails3-jquery-autocomplete 与 rails 3 Web 应用程序一起使用,我希望有一个自动完成的字段,不仅基于在该字段上键入的内容,还基于同一表单中其他字段的值。

这是一个例子:

class Country < ActiveRecord::Base
    has_many :academic_titles_suggestions
end

class AcademicTitleSuggestion < ActiveRecord::Base
    belongs_to :country
end

class People < ActiveRecord::Base
    belongs_to :country
    # has field a field academic_title:string
end

现在当我显示一个人的表格时,我想要一个国家的下拉列表和一个基于该国家/地区的学术头衔建议的学术头衔建议框

你有什么建议吗?

【问题讨论】:

我也直接在项目上问过这个问题:github.com/crowdint/rails3-jquery-autocomplete/issues/140 【参考方案1】:

因此您需要发送额外的字段并考虑将它们用于搜索。

您可以使用:fields 选项发送额外的字段:

f.autocomplete_field :academic_title, fields:  country: "#country" 

那么你需要考虑这个额外的字段进行搜索。

class AcademicTitleController < ActiveRecord::Base
  def autocomplete_academic_title
    country = params[:country]
    title = params[:title]

    titles = AcademicTitleSuggestion.joins(:country).where('countries.name = ? AND academic_title_suggestions.title LIKE ?', country, "%#title%").order(:title)
    render json: titles.map  |title|  id: title.id, label: title.title, value: title.title  
  end
end

更多信息在gem README

【讨论】:

以上是关于通过 rails3-jquery-autocomplete 使用多个输入字段的主要内容,如果未能解决你的问题,请参考以下文章