param丢失或值为空:order

时间:2015-06-22 07:08:44

标签: ruby-on-rails ruby api

我正在使用rails 4中的API创建订单。当我在创建操作中创建订单时,我无法理解以下错误。

Started POST "/api/v1/orders?access_token=testing" for 127.0.0.1 at 2015-06-22 12:23:55 +0530
Processing by Api::V1::OrdersController#create as JSON
  Parameters: {"access_token"=>"testing"}
  ApiKey Load (1.0ms)  SELECT  `api_keys`.* FROM `api_keys`  WHERE `api_keys`.`access_token` = 'testing' LIMIT 1
  Restaurant Load (0.3ms)  SELECT  `restaurants`.* FROM `restaurants`  WHERE `restaurants`.`id` IS NULL LIMIT 1


ActionController::ParameterMissing - param is missing or the value is empty: order:

我的控制器代码:

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 app_sync
  end

  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
        @order = Order.build(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, :subtotal, :final_total, :order_type, :food_item_ids => [])
  end
end

我不确定,我做错了什么。请帮帮我。

3 个答案:

答案 0 :(得分:0)

在您的创建方法中,您使用@order = Order.build(order_params)

在order_params方法中,您期望得到类似关注{&#34; order&#34; =&gt; {customer_id =&gt; 1234,order_id =&gt; 10034} .....}

您的邮递员请求需要更改为具有订单密钥的必要参数。

答案 1 :(得分:0)

你的参数应该是这样的

{"access_token" => "testing", "order" => {"customer_id" => 1, "order_id" => 2, ..} }

此外,为什么在=> []方法结束时order_paramsdef order_params params.require(:order).permit(:customer_id, :order_id, :pos_id, :table_id, :order_number, :order_status,:order_date, :subtotal, :final_total, :order_type, :food_item_ids) end 。它应该只是

{{1}}

答案 2 :(得分:0)

ActionController::Parameters documentation可以看出,当您将:order标记为必需参数时,它必须存在。否则将引发ActionController:ParameterMissing错误。您只需确保将:order与您的请求一起发送,或使用order_params方法审核您的要求。