0

升级到 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

而且由于它是一个哈希,它无法找到带有符号键的值。

所以我尝试了这个,

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 方法似乎运行了两次,第二次未过滤的参数看起来像这样:-

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

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

4

0 回答 0