[英]rails_admin params[:query] in select field is ignored
在 rails admin 中搜索時無法弄清楚如何過濾嵌套關聯。
我在模型中的 rails_admin 配置中有以下代碼。
class Painting < ApplicationRecord
...
rails_admin do
configure :translations, :globalize_tabs
create do
field :author do
searchable [:last_name]
queryable [:last_name]
filterable true
end
field :genre
field :technique
field :material
field :active
field :painting_images
field :author_preview
field :translations
end
end
我嘗試了模型中的每個配置。 但沒有任何效果。 無論我在此下拉輸入中輸入什么,都將被忽略。
我在 puma 服務器中有以下日志。
Started GET "/admin/author?associated_collection=author&compact=true¤t_action=create&source_abstract_model=painting&query=George" for 127.0.0.1 at 2018-03-14 22:07:11 +0300
Processing by RailsAdmin::MainController#index as JSON
Parameters: {"associated_collection"=>"author", "compact"=>"true", "current_action"=>"create", "source_abstract_model"=>"painting", "query"=>"George", "model_name"=>"author"}
Admin Load (0.3ms) SELECT "admins".* FROM "admins" WHERE "admins"."id" = $1 ORDER BY "admins"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Painting Load (0.3ms) SELECT "paintings".* FROM "paintings" WHERE "paintings"."id" IS NULL ORDER BY "paintings"."id" DESC LIMIT $1 [["LIMIT", 1]]
(0.3ms) SELECT COUNT(*) FROM "authors"
Author Load (0.4ms) SELECT "authors".* FROM "authors" ORDER BY authors.id desc LIMIT $1 [["LIMIT", 30]]
Author::Translation Load (0.6ms) SELECT "author_translations".* FROM "author_translations" WHERE "author_translations"."author_id" IN (114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85)
Completed 200 OK in 61ms (Views: 1.4ms | ActiveRecord: 1.9ms)
問題是我在輸入中輸入的所有內容都在參數中進行處理,但從未得到評估。 所以它只是忽略了,它不會通過params[:query]
過濾關聯的集合
我使用globalize
gem 進行翻譯,但不知道如何正確配置 rails admin 以使用它的過濾器。
我確實在原始回購中進行了搜索。 我認為這是錯誤的
def get_collection(model_config, scope, pagination)
...
options = options.merge(query: params[:query]) if params[:query].present?
...
model_config.abstract_model.all(options, scope)
end
此查詢在此處可用,但在此處被忽略。 我嘗試使用model_config.abstract_model.all(options, scope).where(last_name: params[:query])
這很有效。 但這絕對是錯誤的。 有人可以用更通用的方法幫助我解決這個問題嗎?
Rails 管理員將使用為該模型定義的搜索來搜索關聯。 這意味着您無法在 Painting 模型上定義搜索,但您需要在 Author 模型上進行搜索。
因此,在列出作者時有效的任何搜索都將適用於該下拉列表。 您需要注意的另一件事是,默認情況下,rails 管理員會要求 object.title 或 object.name 顯示該名稱。 您可以將它們定義為 Author 模型上的方法。 如果你想要另一種方法,你可以在 config/initializers/rails_admin.rb 上配置 rails admin 使用的內容
RailsAdmin.config do |config|
config.label_methods = [:rails_admin_title, :name]
end
我對rails_admin
和globalize
gems 有同樣的問題。 我通過定義模型的自定義搜索解決了這個問題。 例如
config.model 'Author' do
list do
search_by :search_by_name
field :id
field :name
end
end
在Author
模型中,定義search_by_name
范圍,它將修復作者列表頁面和其他頁面下拉列表中的搜索。
scope :search_by_name, -> (term) {
if term.present?
with_translations.where('author_translations.name like ?', FormatString.new(term, :sql_match).to_s)
else
where({})
end
}
參考: https ://github.com/sferik/rails_admin/wiki/Custom-Search
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.