我正在使用
ruby 3.1.3
、rails 7.0.5
和 activeadmin 3.0
。
我只是按照 activeadmin 网站的安装说明进行操作,但出现以下错误:
ActionView::Template::Error (Ransack needs AdminUser attributes explicitly allowlisted as
searchable. Define a `ransackable_attributes` class method in your `AdminUser`
model, watching out for items you DON'T want searchable (for
example, `encrypted_password`, `password_reset_token`, `owner` or
other sensitive information). You can use the following as a base:
ruby
class AdminUser < ApplicationRecord
# ...
def self.ransackable_attributes(auth_object = nil)
["created_at", "email", "encrypted_password", "id", "remember_created_at", "reset_password_sent_at", "reset_password_token", "updated_at"]
end
# ...
end
):
1: # frozen_string_literal: true
2: insert_tag renderer_for(:index)
我尝试按照说明操作,但遇到了相同的错误,但现在函数内部的数组为空。
刚刚从 active_admin 2.x 升级到 3.0 时遇到了同样的问题。
新版本使用新版本的 ransack (v4),其中包含一项重大更改,因为它现在要求您在模型中显式声明允许的属性和关联列表以进行搜索(更改日志此处)。
将
ApplicationRecord
模型中的所有属性列入白名单将使您的应用程序再次运行:
class ApplicationRecord < ActiveRecord::Base
# ...
def self.ransackable_attributes(auth_object = nil)
authorizable_ransackable_attributes
end
def self.ransackable_associations(auth_object = nil)
authorizable_ransackable_associations
end
end
但是您可能想为代码库上的每个特定模型自定义这些列表。
GoRails 的 Collin Jilbert 在这里发布了一个很棒的解决方案 https://gorails.com/blog/patching-models-for-ransack-4-0-0-extending-activerecord-for-gem-compatibility
滚动至底部查看最终实施。