0

我想在我的控制台中调用它(ap是很棒的打印宝石):

ap Purchase.last(10)

但我收到此错误:

ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash

它是这样工作的:

irb(main):020:0> ap Purchase.last
#<Purchase:0x00007f86b792a320> {
                              :id => 28445,
                         :user_id => 10177,
                      :product_id => nil,
                    :product_type => nil,
                           :price => 9.0,
                    :gateway_code => nil,
                     :gateway_msg => nil,
                :gateway_response => nil,
                      :created_at => Fri, 18 May 2018 22:20:10 UTC +00:00,
                      :updated_at => Fri, 18 May 2018 22:20:10 UTC +00:00,
                  :checkout_total => 9.0,
                      :successful => true,
                         :cart_id => 17242,
                   :report_errors => nil,
    :transacted_value_of_products => 9.0,
            :comp_credits_applied => 0.0
}

没有ap这样的:

irb(main):022:0> Purchase.last(10)
D, [2018-05-25T20:58:54.692575 #70552] DEBUG -- :   Purchase Load (0.5ms)  SELECT  "purchases".* FROM "purchases" ORDER BY "purchases"."id" DESC LIMIT $1  [["LIMIT", 10]]
+-------+---------+------------+-------------+-------+-------------+-------------+-------------+-------------+--------------+-------------+------------+---------+-------------+-------------+-------------+
| id    | user_id | product_id | product_... | price | gateway_... | gateway_msg | gateway_... | created_at  | updated_at   | checkout... | successful | cart_id | report_e... | transact... | comp_cre... |
+-------+---------+------------+-------------+-------+-------------+-------------+-------------+-------------+--------------+-------------+------------+---------+-------------+-------------+-------------+
| 28436 | 10471   |            |             | 5.0   |             | Completed   | {"mc_gro... | 2018-05-... | 2018-05-1... | 5.0         | true       | 17228   | {}          | 5.0         | 0.0         |
| 28437 | 9754    |            |             | 1.99  |             | Completed   | {"mc_gro... | 2018-05-... | 2018-05-1... | 2.48        | true       | 15273   | {}          | 1.99        | 0.0         |
| 28438 | 10472   |            |             | 9.0   |             |             | {\n  "id... | 2018-05-... | 2018-05-1... | 9.0         | true       | 17231   | {}          | 9.0         | 0.0         |
| 28439 | 10348   |            |             | 9.0   |             |             |             | 2018-05-... | 2018-05-1... | 9.0         | true       | 17235   |             | 9.0         | 0.0         |

但不是与争论和ap

irb(main):021:0> ap Purchase.last(3)
ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash
    from (irb):21

事实证明我什么都做不了:

irb(main):023:0> ap Purchase.find(28444)
ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash
    from (irb):23

irb(main):024:0> ap Purchase.find(28444).gateway_response
ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash
    from (irb):24

这是怎么回事?

4

1 回答 1

3

发生了什么以及为什么

ActionController::Parameters(这是params在控制器中使用的)用于继承HashWithIndifferentAccesswhich 继承自Hash. 所以ActionController::Parameters < Hash曾经是真的,就像这样:

params.require(:x).permit(some_hash: %i[key1 key2]).is_a? Hash

如果您正在Hash挖掘params

some_hash = params.require(:x).permit(some_hash: ...)

并在模型中序列化:

class M < ApplicationRecord # Or ActiveRecord::Base in the past
  serialize :h, Hash
end
#...
m.h = some_hash

你最终可能会在你的数据库中得到一些像这样的 YAML:

--- !ruby/object:ActionController::Parameters
...

而不是预期的纯 YAMLized 哈希。

但是随后 Rails5 出现并且ActionController::Parameters不再继承自Hash

  • MakeActionController::Parameters不再继承自HashWithIndifferentAccess.

现在调用to_hor会引发异常to_hashActionController::Parameters

如果您升级代码并尝试加载其中包含序列化数据的模型:

serialize :h, Hash

然后模型将从中加载文本h,解析 YAML 以获取一个ActionController::Parameters实例,并调用to_h它以确保它有一个 Hash,然后你会得到一个异常。

怎么办

您需要做几件事:

  1. 修复您的控制器以确保它们从params.
  2. 修复您的数据,以便您拥有序列化的哈希而不是ActionController::Parameters实例。

修复控制器很简单,只需调用to_unsafe_h尚未真正散列的参数即可。

修复数据更难看。我可能会使用低级数据库接口(即任何地方都没有 ActiveRecord)浏览表,从每一行中读取 YAML,然后YAML.load通过调用将其转换为哈希to_unsafe_h,然后写回the_real_hash.to_yaml文本。您可以在 WHERE 子句中使用like '--- !ruby/object:ActionController::Parameters%'过滤器来仅处理损坏的行。

我还强烈建议serialize您在那里时停止使用。serialize有点杂乱无章,在数据库中使用 YAML 没有理智的方法;现在也几乎不需要它,因为 PostgreSQL 和 MySQL 都具有原生 JSON 支持(但我不确定 ActiveRecord 对 MySQL 的 JSON 的支持程度)。

于 2018-05-26T18:38:28.290 回答