lundi 22 juin 2015

I have three models, Order, order_item, and food_item An order has_many order_items and an order_item has_many food_items

When I create a new order from my order_controller API I am getting the below error which am not sure whats wrong.

NoMethodError Exception: undefined method `order_item' for #<Order:0x00000009dd52a8>

orders_controller.rb

class Api::V1::OrdersController < Api::V1::ApplicationController
protect_from_forgery with: :null_session

skip_before_filter :verify_authenticity_token, :if => Proc.new {|c| c.request.format.json? }
before_action :fetch_restaurent
before_action :fetch_food_items, :only => [:index]

after_action :send_order_to_kitchen, :only => [:create, :update]



  def index
    @orders = Restaurant.orders.fetch_items_from_ethor
  end

  def show
   # Display the list of food_items available from the restaurant
   # Allow the customer to place an order 
  end

    def create  
    debugger
        @order = Order.new(order_params)
        if @order.save
            render :json, order, status:201, location: [:api, current_customer, order]
        else
            render :json, { errors: order.errors }, status:422
        end
    end

  # once the order is placed just send it to the kitechn()
  def update
    @order = Order.find_by(params[:id])
    @rder.update_attributes(order_params)
    if @order.save
      render :json, order, status: 201, location: [:api, current_user.order.id]
    else
      render :json,{errors: order.errors}, status: 422
    end
  end


private
  def order_params
    params.require(:order).permit(:customer_id, :order_id, :pos_id, :table_id, :order_number, 
          :order_status,:order_date,  :food_item_ids)
  end
end

order.rb # model file

class Order < ActiveRecord::Base


  belongs_to :customer
  belongs_to :restaurant
  belongs_to :table
  has_many :order_items
  has_many :food_items, through: :order_items
end

order_item.rb # model

class OrderItem < ActiveRecord::Base

  belongs_to :food_item, inverse_of: :order_items
  belongs_to :order, inverse_of: :order_items
  belongs_to :restaurant
  has_many :food_items
end

food_item.rb # model

class FoodItem < ActiveRecord::Base

  belongs_to :restaurant
  belongs_to :order, inverse_of: :food_items
  belongs_to :order_item, inverse_of: :food_items
  has_many :categories_food_items
  has_many :categories, through: :categories_food_items
end

I do not understand from where it is asking for order_item. Please help me to understand fix this problem.

undefined method `order_item' for #<Order:0x00000009dd52a8>

Aucun commentaire:

Enregistrer un commentaire