我正在努力从3.x版本4.0升级Ransack,其中允许的Ransackable属性应该被明确定义,我已经修复了所有的项目模型,但现在它在Rails核心模块上失败了.

# RuntimeError:
     #   Ransack needs ActionText::RichText attributes explicitly allowlisted as
     #   searchable. Define a `ransackable_attributes` class method in your `ActionText::RichText`
     #   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 ActionText::RichText < ApplicationRecord
     #   
     #     # ...
     #   
     #     def self.ransackable_attributes(auth_object = nil)
     #       ["body", "created_at", "id", "locale", "name", "record_id", "record_type", "updated_at"]
     #     end
     #   
     #     # ...
     #   
     #   end
     #   ```

我已经try 在源代码中直接重新打开类,但这些更改没有被Rails获取和忽略.我试图在初始化期间找到一些配置更改,但这也不起作用.我打赌有人已经解决了从3.0到4.x的迁移问题

已经测试过的内容

  1. 创建低于app/models/action_text/rich_text.rb的模型文件
class ActionText::RichText < ApplicationRecord

  # ...

  def self.ransackable_attributes(auth_object = nil)
    ["body", "created_at", "id", "locale", "name", "record_id", "record_type", "updated_at"]
  end

  # ...

end

结果-&>只是忽略了,相同的问题Rails看不到这些更改.

  1. 更新config/initializers/action_text.rb以下的初始值设定项
    class ActionText::RichText < ActiveRecord::Base
      def self.ransackable_attributes(auth_object = nil)
        ["body", "created_at", "id", "locale", "name", "record_id", "record_type", "updated_at"]
      end
    end

初始化加载过程中出现结果错误-&gt;

An error occurred while loading rails_helper.
Failure/Error: require File.expand_path('../config/environment', __dir__)

NoMethodError:
  undefined method `has_many_attached' for ActionText::RichText:Class
# ./config/initializers/action_text.rb:7:in `<main>'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:6:in `require'
# ./spec/rails_helper.rb:6:in `<top (required)>'
No examples found. 

推荐答案

看起来你需要把这个日志(log)写出来-明确定义建议的方法.因为有这样的继承:ActionText::RichText < ActionText::Record < ActiveRecord::Base,所以可以在父类ActionText::RichText中定义这个方法

# config/initializers/action_text.rb

class ActionText::Record < ActiveRecord::Base
  def self.ransackable_attributes(auth_object = nil)
    authorizable_ransackable_attributes
  end
end

也可以在初始化式中直接在ActionText::RichText类中定义此方法.但由于加载顺序(此DSL方法尚不可用),可能会引发'method_missing': undefined method 'has_many_attached' for ActionText::RichText:Class (NoMethodError)(问题文本中的错误)

为了避免它,你可以使用ActiveSupport钩子,它的名字可以找到here

在本例中,补丁将如下所示

# config/initializers/action_text.rb

ActiveSupport.on_load(:action_text_rich_text) do
  class ActionText::RichText < ActionText::Record
    def self.ransackable_attributes(auth_object = nil)
      authorizable_ransackable_attributes
    end
  end
end

当然,您可以使用所需属性的显式数组(字符串数组),如%w[id body name record_id record_type]等,而不是authorizable_ransackable_attributes.

Ruby-on-rails相关问答推荐

form_with正在try 用错误的路径生成操作

HTTP:MimeNegotiation::InvalidType(html不是有效的MIME类型):""

我使用rbenv安装Ruby,但我在使用";gem";->;错误:执行gem时遇到此错误.(Errno::EACCES)权限被拒绝

刺激中的Rails 7和 bootstrap 错误-无法解析模块说明符 bootstrap

查询Where Date.parse Rails

在 Rails 6.1 升级后,作业(job)规范使用 Minitest::UnexpectedError 包装错误

如何从设计中删除/禁用注册

键入rails 控制台无法启动?

attr_accessible(*attributes) 和 attr_protected(*attributes) 有什么区别?

使用brew 软件安装了 elasticsearch,但找不到配置文件

OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv3 read server hello A

具有条件的列的计数器缓存?

在 Rails/ActiveRecord 列名中使用问号字符

如何在 rspec 测试中输出变量?

Rspec/Capybara 正在加载,循环要求被认为是有害的

在 Rails 中创建随机 DateTime 的最佳方法

Rails 5.1:未知 firstpos:NilClass - 问题重新加载应用程序

如何在 RSpec 中包含 Rails 助手

Rails 路由:GET 没有参数 :id

按照给定 ID 数组的顺序按 ID 查找模型记录