ruby-on-rails - rails autocomplete gem 以限制对语言环境参数的搜索
问题描述
自动完成 gem 调用以下操作进行处理
Started GET "/nations/autocomplete_nationtranslation_name?locale=en&term=erm"
Nationtranslation 表有一个索引列
t.string "locale"
模型定义为:
autocomplete :nationtranslation, :nome, full: true
autocomplete :nationtranslation, :nome, full: true, :extra_data => [:locale]
两者都返回所有语言环境的所有可能值,而
autocomplete :nationtranslation, :nome, full: true, :extra_data => params[:locale]
返回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
推荐阅读
- c++ - std::async 执行顺序保证
- python - 将深度反投影到原始网格上
- swift - SwiftUI - 将删除操作(带索引)添加到警报按钮
- python - GPUtil.showUtilization 突然不起作用
- reactjs - 在 .env 中隐藏我的 MongoURI 变量会破坏我在 Heroku 上的应用
- firebase - Flutter Firebase - 未能正确删除经过 Google 身份验证的用户
- c# - .Net Core Worker Service 在作为 Windows 服务运行时找不到 log4net.config
- javascript - 获取简单 GET 请求的 NS_BINDING_ABORTED 错误
- c++ - 将数组传递给类方法以进行选择排序
- python - 显示 SyntaxError: invalid syntax 的交互式 Shell 打印函数