1

我有以下charge.rb:

class Charge < ActiveRecord::Base

  serialize :response, Hash

  include HTTParty
  belongs_to :order  

  def charge_url
    return "#{Pin.base_url}/1/charges"
  end

  def submit(ip)
    @authorization = {:username => Pin.secret_key, :password => Pin.public_key}
    @params =  {
      'currency' => "USD",
      'description' => "One Testivate Retail Site Search Benchmark report",
      'amount' => 99900,
      'ip_address' => (ip),
      'email' => self.order.user.email,
      'card' => {'number' => self.number,
                'expiry_month' => self.expiry_month,
                'expiry_year' => self.expiry_year,
                'cvc' => self.cvc,
                'name' => self.name,
                'address_line1' => self.address_line1,
                'address_line2' => self.address_line2,
                'address_city' => self.address_city,
                'address_postcode' => self.address_postcode,
                'address_state' => self.state,
                'address_country' => self.country }}
    response = HTTParty.post(self.charge_url, :query => @params, :basic_auth => @authorization)
    self.update_attributes :token => response["response"]["token"], :display_number => response["response"]["display_number"], :response => response
  end    

end

当我在控制台中调用 self 的提交方法时,我得到了这个:

[30] pry(#<Charge>)> self
+----+----------+---------+-----------+-----------+----------+----------+-----------+----------+----------+----------+----------+----------+-----+----------+----------+----------+----------+----------+-------+----------+----------+
| id | order_id | user_id | token_... | token     | displ... | creat... | update... | card_... | custo... | number   | expir... | expir... | cvc | name     | addre... | addre... | addre... | addre... | state | country  | response |
+----+----------+---------+-----------+-----------+----------+----------+-----------+----------+----------+----------+----------+----------+-----+----------+----------+----------+----------+----------+-------+----------+----------+
| 2  | 9        |         |           | ch_O5d... |          | 2013-... | 2013-1... |          |          | 55200... | 12       | 2014     | 123 | Rolan... | 42 Se... |          | Lathlain |          | 6454  | Austr... | {"res... |
+----+----------+---------+-----------+-----------+----------+----------+-----------+----------+----------+----------+----------+----------+-----+----------+----------+----------+----------+----------+-------+----------+----------+
1 row in set
[31] pry(#<Charge>)> self.save
NoMethodError: undefined method `name' for nil:NilClass
from /Users/steven/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.11/lib/active_record/transactions.rb:274:in `rescue in rollback_active_record_state!'
down
Error: At bottom of stack, cannot go further!
[33] pry(#<Charge>)> up

Test failure: 

Frame number: 1/97
Frame type: method

From: /Users/steven/.rvm/gems/ruby-1.9.2-p320/gems/pry-remote-0.1.7/lib/pry-remote.rb @ line 284 Object#remote_pry:

    283: def remote_pry(host = PryRemote::DefaultHost, port = PryRemote::DefaultPort)
 => 284:   PryRemote::Server.new(self, host, port).run
    285: end

如果我再次向上,我最终会进入提交方法。在 charge.rb 模型中没有明确定义回调。我到底怎么才能找到不会保存的 NilClass?

4

1 回答 1

0

像这样修复它:

self.update_attributes :token => response["response"]["token"], :display_number => response["response"]["display_number"], :response => response.to_hash

(无论如何,响应是一个哈希,但添加.to_hash工作,嘿,这很好。)

于 2013-10-09T22:48:52.547 回答