【问题标题】:Use multiple input fields with rails3-jquery-autocomplete通过 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

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

你有什么建议吗?

【问题讨论】:

标签: jquery ruby-on-rails autocomplete


【解决方案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

【讨论】:

    猜你喜欢
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2011-04-21
    • 1970-01-01
    相关资源
    最近更新 更多