OrdersController中的NoMethodError #create

时间:2014-05-06 09:19:52

标签: ruby-on-rails activemerchant

我正在构建一个简单的购物应用程序,并试图使用Ryan Bates的“#145集成活跃商家”来指导建立结帐流程。

订单型号:

class Order < ActiveRecord::Base

has_many :order_products
has_many :products, through: :order_products

attr_accessor :card_number, :security_code, :card_expires_on


def credit_card

    @credit_card ||= ActiveMerchant::Billing::CreditCard.new(
        :first_name             => first_name,
        :last_name              => last_name,
        :card_number            => card_number,
        :verification_value     => security_code,
        :month                  => card_expires_on{month},
        :year                   => card_expires_on{year}
        )
end


def self.purchase(basket)

    response = GATEWAY.purchase(Product.total_basket_price(basket), credit_card)

end




end

Orders_controller:

    def create

    @order = Order.new

    basket.each do |item_id|
        @order.order_products.build(product: Product.find(item_id))
    end

    if @order.save!
        if @order.purchase
            render "show"
        else
            render "new"
        end
    else
        render "new"
    end

end

产品型号:

  def self.total_basket_price(basket)
    where(id: basket.to_a).sum(:price)*100
  end

当我点击提交时,我收到以下错误消息:

  

OrdersController中的NoMethodError #create

     

未定义的方法`购买'

1 个答案:

答案 0 :(得分:2)

purchase是类记录,但您尝试在Order的实例上调用它。此外,此方法需要一个参数,但您尝试在没有它们的情况下调用它。