2013-10-08 138 views
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 

當我打電話在控制檯上自提交方法,我得到這個:

[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?

+0

做你的self.name方法在某種程度上使用另一種模式比如「user.name」或者是將調用委託給這個模型的東西,因此在這種情況下委託給nil? – jethroo

+0

我沒有明確定義@ charge.name。這只是收費表中的一個字符串列。 –

回答

0

固定這樣的:

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

(的反應是哈希反正,但加入.to_hash工作這麼哎這很好。)