控制器上的类filter_parameters方法获取Hash而不是HashWithIndifferentAccess

时间:2013-07-10 05:31:31

标签: ruby-on-rails ruby-on-rails-3.2

升级到Rails 3.2.13后,我开始遇到此问题。

我的控制器中有这个filter_parameters方法覆盖,如此

class Api::OrdersController < ApiController
  before_filter :protect_api

  def filter_parameters(unfiltered)
    filtered[:credit_card] = unfiltered[:credit_card].dup
    filtered[:credit_card][:number] = '[FILTERED]'
    filtered[:credit_card][:type] = '[FILTERED]'
    filtered
  end

在我的规范中我有这个

put "/api/orders/#{order.number}",{credit_card: {
                                          number: '424242466624242',
                                          month: 9,
                                          year: Time.now.year + 1,
                                          first_name: 'bob',
                                          last_name: 'Joe',
                                          verification_value: '123',
                                          type: 'visa'
                                },

当我运行规范时出现错误

Failure/Error: put "/api/orders/#{order.number}",{credit_card: {
     TypeError:
       can't dup NilClass

所以我追溯到这一行

unfiltered[:credit_card].dup
puts unfiltered.class #Hash

由于它是Hash,因此无法使用符号化键找到该值。

所以我试过了,

def filter_parameters(unfiltered)
    unfiltered = unfiltered.with_indifferent_access
    filtered = unfiltered.dup
    filtered[:credit_card] = unfiltered[:credit_card].dup
    filtered[:credit_card][:number] = '[FILTERED]'
    filtered[:credit_card][:type] = '[FILTERED]'
    filtered
end

但是filter_parameters方法似乎运行了两次,而第二次未过滤的param看起来像这样: -

{"session_id"=>"c050d509b93f28d10b81976fcaf02d81", "timezone"=>"pst"}

知道如何解决这个问题吗?

0 个答案:

没有答案