找不到'id'=的订单

时间:2017-12-23 06:55:53

标签: ruby-on-rails shopping-cart

我确实关闭了浏览器但仍然渲染,本地主机服务器重新启动但错误仍然存​​在。

module ApplicationHelper
    def current_order
        if session[:order_id].nil?
            Order.find(session[:order_id])
        else
            Order.new
        end
    end
end
class OrderItemsController < ApplicationController
    def create
        @order = current_order
        @order_item = @order.order_items.new(order_item_params)
        @order.save
        session[:order_id] = @order.id
    end

    def update
        @order = current_order
        @order_item = @order.order_item.find(params[:id])
        @order_item.update(order_item_params)
        @order_items = @order.order_items

    end
    def destroy
        @order = current_order
        @order_item = @order.order_item.find(params[:id])
        @order_item.destroy
        @order_items = @order.order_items
    end
private
    def order_item_params
        param.require(:order_item).permit(:product_id, :p_quantity)
    end
end

我在哪里做错了??

get '/products' => 'products#home'
  resources :products
  resources :order_items
  resources :carts, only: [:show]
  root 'products#home'

我通过rails控制台创建了一个订单,但错误仍在渲染.. #help 任何详细的答案..?

1 个答案:

答案 0 :(得分:1)

问题在于current_order方法,您将nil值传递给查找方法Order.find(nil)。您需要像这样交换ifelse语句

def current_order
  if session[:order_id].nil?
    Order.new
  else
    Order.find(session[:order_id])   
  end
end