rails 自动完成 gem 以限制对区域设置参数的搜索

rails autocomplete gem to limit searches on locale parameter

自动完成调用以下操作进行处理gem

Started GET "/nations/autocomplete_nationtranslation_name?locale=en&term=erm"

Nationtranslation table 有一个索引列

t.string   "locale"

模型定义为:

autocomplete :nationtranslation, :nome, full: true
autocomplete :nationtranslation, :nome, full: true, :extra_data => [:locale]

两个 return 所有语言环境的所有可能值,而

autocomplete :nationtranslation, :nome, full: true, :extra_data => params[:locale]

returns ActionController::RoutingError (undefined local variable or method 'params' for NationsController:Class

如何自动完成运行范围为
的搜索 locale = params[:locale]

我看到的唯一方法是提供您自己的控制器操作方法,该方法默认由 rails-jquery-autocomplete gem 生成。

class NationsController < ApplicationController

  # you don't really need this anymore
  # autocomplete :nationtranslation, :name

  def autocomplete_nationtranslation_name
    translations = Nationtranslation.where("name LIKE ? AND locale = ?", 
                                           "%#{params[:term]}%", 
                                           params[:locale])
                                    .order(:name)
    render json: translations.map { |t| {id: t.id, label: t.name, value: t.name} }    
  end
end