Rails:未定义方法“total_price”对于 nil:NilClass
我正在使用 Rails 3.0.4 构建一个音乐会门票销售应用程序,主要使用敏捷 Web 开发教程 (http://pragprog.com/titles/rails3/agile-web-development-with-rails) 并尝试合并Ryan Bate 的订单购买方法(http://railscasts.com/episodes/146-paypal-express-checkout)。一切都适用于orders_controller.rb中的以下内容:
def create
@order = Order.new(params[:order])
@order.add_line_items_from_cart(current_cart)
@order.ip_address = request.remote_ip
respond_to do |format|
if @order.save
Notifier.order_received(@order).deliver
format.html { redirect_to(calendar_url, :notice => 'Thank you for your order.') }
format.xml { render :xml => @order, :status => :created, :location => @order }
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
else
format.html { render :action => "new" }
format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
end
end
但是当我将“&&@order.purchase”添加到条件子句时,order.rb模型如下:
class Order < ActiveRecord::Base
#...
belongs_to :cart
#...
def price_in_cents
(cart.total_price*100).round
end
def purchase
response = GATEWAY.purchase(price_in_cents, credit_card, purchase_options)
cart.update_attribute(:purchased_at, Time.now) if response.success?
response.success?
end
#...
end
我收到一个“未定义的方法‘total_price’为零: NilClass”错误。我可以通过添加到订单“create”方法来解决这个问题
@order = current_cart.build_order(params[:order])
,但这会以某种方式阻止相关订单信息(在本例中为“@order.line_items”)在电子邮件文本中呈现,从而弄乱了“order_received”通知。
“cart”对象在途中的某个地方被设置为 nil,但是
Cart.destroy(session[:cart_id])
从订单“create”方法中删除并不能解决问题。
有人知道这个菜鸟的线索吗?
I'm building a concert ticket sales application with Rails 3.0.4, working primarily with the Agile Web Development tutorial (http://pragprog.com/titles/rails3/agile-web-development-with-rails) and trying to incorporate Ryan Bate's order purchase method (http://railscasts.com/episodes/146-paypal-express-checkout). Everything works with the following in orders_controller.rb:
def create
@order = Order.new(params[:order])
@order.add_line_items_from_cart(current_cart)
@order.ip_address = request.remote_ip
respond_to do |format|
if @order.save
Notifier.order_received(@order).deliver
format.html { redirect_to(calendar_url, :notice => 'Thank you for your order.') }
format.xml { render :xml => @order, :status => :created, :location => @order }
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
else
format.html { render :action => "new" }
format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
end
end
But when I add "&& @order.purchase" to the conditional clause, with the order.rb model as follows:
class Order < ActiveRecord::Base
#...
belongs_to :cart
#...
def price_in_cents
(cart.total_price*100).round
end
def purchase
response = GATEWAY.purchase(price_in_cents, credit_card, purchase_options)
cart.update_attribute(:purchased_at, Time.now) if response.success?
response.success?
end
#...
end
I receive an "undefined method `total_price' for nil:NilClass" error. I can get around this by adding
@order = current_cart.build_order(params[:order])
to the orders "create" method, but this messes up the "order_received" notification by somehow preventing the pertinent order information (in this case "@order.line_items") from rendering in the e-mail text.
The "cart" object is being set to nil somewhere along the way, but removing
Cart.destroy(session[:cart_id])
from the order "create" method does not fix the problem.
Anyone got a clue for this noob?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来购物车对象实际上并未在belongs_to关系中指定,您需要执行
@order.cart = current_cart
或current_cart.order = Order.new
code> 或类似的内容。It doesn't look like the Cart object is ever actually specified in the belongs_to relation, you need to either do
@order.cart = current_cart
, orcurrent_cart.order = Order.new
, or something along those lines.