【问题标题】:rails_admin params[:query] in select field is ignored选择字段中的 rails_admin params[:query] 被忽略
【发布时间】:2018-03-14 19:18:54
【问题描述】:

在 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&current_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 以使用它的过滤器。

我在原始存储库中进行了搜索。 我觉得有点不对this

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]) 这是有效的。但这绝对是错误的。有人可以帮助我用更通用的方法解决这个问题吗?

【问题讨论】:

  • 作者的姓氏是否显示在下拉列表中?如果不是,则需要为 RailsAdmin 重新定义author_name 方法
  • 我发现了一个丑陋的黑客来解决这个问题。如果有人会遇到这个问题,我可以提供不是很好的解决方案。

标签: ruby-on-rails rails-admin globalize


【解决方案1】:

rails_adminglobalize 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

【讨论】:

    【解决方案2】:

    Rails 管理员将使用为该模型定义的搜索来搜索关联。 这意味着您无法在“绘画”模型上定义搜索,但需要在“作者”模型上进行。

    因此,在列出作者时有效的任何搜索都将在该下拉列表中有效。 您需要注意的另一件事是 Rails 管理员默认会要求 object.title 或 object.name 显示该名称。 您可以将它们定义为 Author 模型上的方法。 如果您想要其他方法,您可以在 config/initializers/rails_admin.rb 上配置 rails 管理员使用的内容

    RailsAdmin.config do |config|
      config.label_methods = [:rails_admin_title, :name]
    end
    

    【讨论】:

    • 这不起作用。 object_label_method { :last_name } 在我的 author.rb 中并在列表中显示为 Smith,请在此处查看屏幕截图 take.ms/wFHs4
    • 尝试在你的 author.rb 模型上定义 title 方法
    • def title last_name end 没有成功。 :( 它仍然只有 params[:query] 但在涉及 sql 语句时会忽略。
    • 您是否尝试在 Author 模型上定义列表块?通过在作者列表中定义字段来搜索作者在作者列表中的工作,您将解决下拉搜索。
    猜你喜欢
    • 2013-04-06
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    相关资源
    最近更新 更多