使用Rails完成书籍Agile Web Development 4.我试图通过他们的沙箱API实现Paypal支付。但在创建订单时,order
会出现错误。
我有一个错误:未定义的方法order
class Order < ActiveRecord::Base
PAYMENT_TYPES = [ "Check", "Credit card", "Purchase order" ]
has_many :line_items, dependent: :destroy
# ...
validates :name, :address, :email, presence: true
validates :pay_type, inclusion: PAYMENT_TYPES
def add_line_items_from_cart(cart)
cart.line_items.each do |item|
item.cart_id = nil
line_items << item
puts line_items
end
end
serialize :notification_params, Hash
def paypal_url(return_path)
values = {
business: "imanov123-facilitator@gmail.com",
cmd: "_xclick",
upload: 1,
return: "#{Rails.application.secrets.app_host}#{return_path}",
invoice: id,
amount: order.line_items.price,
item_name: order.line_items.product.title,
item_number: order.line_items.id,
quantity: order.line_items.quantity,
notify_url: "#{Rails.application.secrets.app_host}/hook"
}
"#{Rails.application.secrets.paypal_host}/cgi-bin/webscr?" + values.to_query
end
end
来自我的订单控制器
# POST /orders
def create
@order = Order.new(order_params)
@order.add_line_items_from_cart(@cart)
respond_to do |format|
if @order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
OrderNotifier.received(@order).deliver
format.html { redirect_to @order.paypal_url(order_path(@order)), notice:
'Thank you for your order.' }
else
format.html { render action: 'new' }
end
end
end
NameError (undefined local variable or method `order' for #<Order:0xb59cca4c>):
app/models/order.rb:23:in `paypal_url'
app/controllers/orders_controller.rb:59:in `block (2 levels) in create'
app/controllers/orders_controller.rb:54:in `create'
答案 0 :(得分:2)
您需要在self
上调用属性,因为paypal_url
是Order
类对象上的实例方法,而您的@order
变量恰好是:{ {1}}。 @order = Order.new(order_params)
方法确实没有order
变量。
paypal_url