NoMethodError in OrderController#new

I'm taking an online Web Development class and unfortunately the instructor does not respond to emails. During this assignment I'm running into an error when trying to create a checkout screen for my shopping cart. The thing is, the professor also didn't bother to check his code and see if it was working, so I have no correct solution to reference. I'm assuming that there might be an issue with the book, as it seems others are having similar issues.

I'm a total noob with rubyonrails, so I'm looking for some help to find the cause of this error.

I can clearly see the NoMethodError in OrderController#new. But have no idea what that means I should do.

Any guidance for me?

error

order_controller.rb

class OrdersController < ApplicationController
  include CurrentCart
  before_action :ensure_cart_isnt_empty, only: :new
  before_action :set_cart, only: [:new, :create]
  before_action :set_order, only: [:show, :edit, :update, :destroy]

  # GET /orders
  # GET /orders.json
  def index
    @orders = Order.all
  end

  # GET /orders/1
  # GET /orders/1.json
  def show
  end

  # GET /orders/new
  def new
    @order = Order.new
  end

  # GET /orders/1/edit
  def edit
  end

  # POST /orders
  # POST /orders.json
  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
        format.html { redirect_to store_index_url, notice:'Thank you for your order.' }
        format.json { render :show, status: :created, location: @order } 
      else 
        format.html { render :new } 
        format.json { render json: @order.errors, status: :unprocessable_entity } 
      end 
    end 
  end

  # PATCH/PUT /orders/1
  # PATCH/PUT /orders/1.json
  def update
    respond_to do |format|
      if @order.update(order_params)
        format.html { redirect_to @order, notice: 'Order was successfully updated.' }
        format.json { render :show, status: :ok, location: @order }
      else
        format.html { render :edit }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /orders/1
  # DELETE /orders/1.json
  def destroy
    @order.destroy
    respond_to do |format|
      format.html { redirect_to orders_url, notice: 'Order was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def ensure_cart_isnt_empty
    if @cart.line_items.empty?
      redirect_to store_index_url, notice: 'Your cart is empty'
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_order
      @order = Order.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def order_params
      params.require(:order).permit(:name, :addresss, :email, :pay_type)
    end

end


ANSWERS:


The @cart object is nil, so when you try to call line_items (l. 73) you get an error because that method is not included on the NilClass.

So it seems that the @cart hasn't been initialized. I would need to see the whole OrderController code to be more accurate, but probably the ensure_cart_isnt_empty method is expecting the object to be initialized by some before_filter method. Or maybe the database is not returning anything.

Edit:

After looking at your code, I think the problem is in the following lines:

  before_action :ensure_cart_isnt_empty, only: :new
  before_action :set_cart, only: [:new, :create]

Please note that before methods are executed in order, so when ensure_cart_isnt_empty is executed, @cart is not initialized. You would need to change it as follows:

  before_action :set_cart, only: [:new, :create]
  before_action :ensure_cart_isnt_empty, only: :new

So set_car will run first and ensure_cart_isnt_empty will find a valid object.



 MORE:


 ? Object with key assigned to a function returning undefined but console.log shows the data
 ? jQuery Array Undefined Error
 ? Undefined index: transfer_encoding
 ? PHP undefined offset in array looping
 ? Missing element in the function
 ? Ionic 2: Use cordova plugin with Typescript that has its own namespace
 ? Why is my variable undefined
 ? Difference between [undefined × 2] and [undefined, undefined] in JavaScript
 ? php append non existing element to string
 ? Javascript array of objects output is undefined , not as expected data.data.field