我有以下控制器
class PaypalOrdersController < Spree::BaseController
def new
@paypal_order = PaypalOrder.new
end
def create
@order1 = current_order
@paypal_order = PaypalOrder.new(params[:paypal_order])
if @paypal_order.save
if @paypal_order.purchase
render :action => "success"
else
render :action => "failure"
end
else
render :action => "new"
end
end
end
对应的模型是:
class PaypalOrder < ActiveRecord::Base
require 'paypal_payment'
belongs_to :order
attr_accessor :card_number, :card_verification
validate :validate_card, :on => :create
def purchase
orderHash = @order1.clone
paypalHash = @paypal_order.clone
PaypalPayment.new(orderHash, paypalHash)
end
private
def validate_card
#some code
end
def credit_card
#some code
end
end
当购买方法被触发时,我收到错误cannot clone nil class
.在调试时,我发现@order1
和@paypal_order
在purchase
方法中都是零。我不确定为什么会发生这种情况。请有人解释一下。
谢谢
控制器:
PaypalOrder.purchase @order1, @paypal_order
型:
def self.purchase order, paypal_order
orderHash = order.clone
paypalHash = paypal_order.clone
PaypalPayment.new(orderHash, paypalHash)
end
编辑:如何使用Ruby on Rails将数据从控制器传递到模型?
试试这个
控制器
if @paypal_order.purchase @order1
型
def purchase order1
orderHash = order1.clone
paypalHash = self.clone
PaypalPayment.new(orderHash, paypalHash)
end