【发布时间】:2018-05-26 01:00:34
【问题描述】:
我想在我的控制台中调用它(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
发生了什么事?
【问题讨论】:
-
数据库中的
gateway_response是什么?序列化params或许? -
我问的原因是因为
ActionController::Parameters曾经继承自Hash,所以你可以serialize :stuff, Hash,将params放入stuff,最后得到一团YAML在标记为ActionController::Parameters实例的数据库中。但是ActionController::Parameters不再继承自Hash并且它的to_h方法引发了异常。结果是,如果您没有非常小心,更改 Rails 版本可能会使您的数据库中充斥着损坏的数据。 -
@muistooshort 就是这样。我并不是所有的购买都有网关响应,因为购买可以通过不同的方式进行。一些网关响应来自 Authorize.net(旧的),一些来自 Stripe,一些来自 Paypal。根据我的代码,这些序列化响应具有不同的类,正如您所说,看起来来自 Paypal 的响应被序列化为 ActionController::Parameters。我最近将我的应用程序从 4.2 升级到了 5.2,看起来我需要清理我的数据库。随时发表您的评论作为答案,我会接受。
标签: ruby-on-rails rails-activerecord strong-parameters awesomeprint