订单控制器和活跃商家出现问题

发布于 2024-11-07 00:25:19 字数 1721 浏览 0 评论 0原文

我将购物车存储在数据库中,并在应用程序帮助程序中有一个 current_cart 方法。

 private

    def current_cart 
      Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound 
      cart = Cart.create 
      session[:cart_id] = cart.id 
      cart
   end

每个购物车有_许多行_商品。当客户准备结帐时,他们会被发送到 order/new ...但是,当订单从支付网关成功返回时,一切都很好。但我正在测试订单失败,我的 create 控制器似乎有问题,因为 @order 对象没有被保存,并且 Rails 正在尝试渲染 <再次代码>order#new。它甚至没有尝试看看付款是否成功。我不确定出了什么问题,因为几天前我测试它时它正在工作。

def create
    @order = current_cart.build_order(params[:order])
    @order.ip_address = request.remote_ip

    @order.total_tokens = @order.calculate_total_tokens
    @order.user_id = current_user

      if @order.save
        if @order.purchase
         render :action => "success"
        else
          render :action => "failure"
        end
      else
        render :action => "new"
      end
   end

 def new
    @cart = current_cart
    if @cart.line_items.empty?
        redirect_to store_url, :notice => "Your cart is empty."
        return
    end

     @order = Order.new

     respond_to do |format|
       format.html # new.html.erb
       format.xml  { render :xml => @order }
     end
  end

当它尝试渲染页面时,我收到此错误,显然是因为订单尚未保存:

NoMethodError in Orders#create

Showing /Users/justin/Code/trackable/app/views/orders/_order_cart.html.erb where line #1     raised:

undefined method `line_items' for nil:NilClass
Extracted source (around line #1):

1: <% unless @cart.line_items.empty? %>
2: <div id="checkout_cart">
3: <% @cart.line_items.each do |l| %>
4:   <%= l.quantity %>

不确定发生了什么

I'm storing my shopping cart in the database and have a current_cart method in my application helper.

 private

    def current_cart 
      Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound 
      cart = Cart.create 
      session[:cart_id] = cart.id 
      cart
   end

Each cart has_many line_items. When a customer is ready to check out, they are sent to order/new ... when the order returns successful from the payment gateway though, everything is fine. But I'm testing the order failure and it seems there's a problem with my create controller, because the @order object isn't being saved and rails is trying to render order#new again. It's not even getting to try and see if the payment was successful. I'm not sure what' wrong, as it was working when I tested it a few days ago.

def create
    @order = current_cart.build_order(params[:order])
    @order.ip_address = request.remote_ip

    @order.total_tokens = @order.calculate_total_tokens
    @order.user_id = current_user

      if @order.save
        if @order.purchase
         render :action => "success"
        else
          render :action => "failure"
        end
      else
        render :action => "new"
      end
   end

 def new
    @cart = current_cart
    if @cart.line_items.empty?
        redirect_to store_url, :notice => "Your cart is empty."
        return
    end

     @order = Order.new

     respond_to do |format|
       format.html # new.html.erb
       format.xml  { render :xml => @order }
     end
  end

I'm getting this error when it tries to render the page, obviously because the order hasn't been saved:

NoMethodError in Orders#create

Showing /Users/justin/Code/trackable/app/views/orders/_order_cart.html.erb where line #1     raised:

undefined method `line_items' for nil:NilClass
Extracted source (around line #1):

1: <% unless @cart.line_items.empty? %>
2: <div id="checkout_cart">
3: <% @cart.line_items.each do |l| %>
4:   <%= l.quantity %>

Not sure what's going on

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒗幽 2024-11-14 00:25:19

因此,将 @cart = current_cart 添加到 #create 控制器似乎已经解决了问题。 ActiveMerchant 抛出了验证错误,并且出于某种原因,当它这样做时,它正在丢失 @cart ...当我测试 ActiveRecord 验证时,它并没有这样做,所以我仍然不太确定我改变了什么,把一切搞砸了。

So, adding @cart = current_cart to the #create controller seems to have fixed the problem. ActiveMerchant was throwing a validation error and for some reason, when it did that, it was losing @cart ... it wasn't doing that earlier when I was testing ActiveRecord validations, so I'm still not quite sure what I changed that screwed everything up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文