无法访问模型中的控制器变量

时间:2014-04-04 07:21:39

标签: ruby-on-rails ruby-on-rails-3

我有以下控制器

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_orderpurchase均为零。我不确定为什么会这样。请有人解释一下。

由于

2 个答案:

答案 0 :(得分:4)

控制器:

PaypalOrder.purchase @order1, @paypal_order

模型:

def self.purchase order, paypal_order
 orderHash = order.clone
 paypalHash = paypal_order.clone
 PaypalPayment.new(orderHash, paypalHash)
end

编辑: How do you pass data from a controller to a model with Ruby on Rails?

答案 1 :(得分:0)

试试这个

控制器

if @paypal_order.purchase @order1

模型

def purchase order1
  orderHash = order1.clone
  paypalHash = self.clone        
  PaypalPayment.new(orderHash, paypalHash)
end