ActionController::ParameterMissing(参数缺失或值为空:vendor

ActionController::ParameterMissing (param is missing or the value is empty: vendor

我正在尝试在我的 Rails 应用程序上使用 Postman 创建一个 post,但我遇到一个问题:

ActionController::ParameterMissing (param is missing or the value is empty: vendor
Did you mean?  vendor_data
               action
               quote_type
               controller):

这是我的 controller 文件,其中调用了参数:

def create_vendor_params
    params.require(:vendor).permit(
      :vendor_id, :quote_type, :first_name, :last_name, :email, :phone_type, :phone, :request_type, :address1, :address_city, :address_state, :address_zip, :DOB
    )
  end

这是我从 Postman 发送的数据。现在我不确定是不是因为 renter_data 它嵌套在我的 params permit:

{
    "vendor_id": 2,
    "quote_type": "renter",
    "renter_data": {
        "first_name":"Tony",
        "last_name":"Stark",
        "email":"tony@starkindustries",
        "phone_type":"mobile",
        "phone":"6504881234",
        "request_type":"renter",
        "address1":"123 Main Street",
        "address_city":"Palo Alto",
        "address_state":"CA",
        "address_zip":"94301",
        "DOB":"1990-07-22"
    }
}

这个问题是因为 renter_data 中的嵌套数据吗?如果是,我该如何嵌套它?

编辑

当我创建 post 时,我在请求末尾看到一个空对象:

Processing by Api::V2::LeadController#create as */*
      Parameters: {"vendor_id"=>2, "quote_type"=>"renter", "renter_data"=>{"first_name"=>"Tony", "last_name"=>"Stark", "email"=>"tony@starkindustries", "phone_type"=>"mobile", "phone"=>"6504881234", "request_type"=>"renter", "address1"=>"123 Main Street", "address_city"=>"Palo Alto", "address_state"=>"CA", "address_zip"=>"94301", "DOB"=>"1990-07-22"}, "vendor"=>{} <---HERE }

这是为什么?

这是create_vender_params函数中嵌套强参数的概念你必须添加更多允许的参数

params.require(:vendor).permit(
  :vendor_id, :quote_type, renter_Data: [:first_name, :last_name, :email, :phone_type, :phone, :request_type, :address1, :address_city, :address_state, :address_zip, :DOB])

有两个问题:首先,强参数期望输入嵌套在vendor下。其次,强参数不期望嵌套 renter_data 散列。

要在您的查询中解决此问题,请将请求数据的结构更改为:

{
  "vendor": {
    "vendor_id": 2,
    "quote_type": "renter",
    "first_name":"Tony",
    "last_name":"Stark",
    "email":"tony@starkindustries",
    "phone_type":"mobile",
    "phone":"6504881234",
    "request_type":"renter",
    "address1":"123 Main Street",
    "address_city":"Palo Alto",
    "address_state":"CA",
    "address_zip":"94301",
    "DOB":"1990-07-22"
  }
}

如果你想保留嵌套的 renter_data 散列,那么你必须更改像 Paarth Agrawal 已经建议的强参数。但是你仍然需要将所有内容嵌套在 vendor.

vendor_table 有一些字段 vendor_id, quote_type, first_name, last_name, email,phone_type,phone, request_type, address1,address_city,address_state, address_zip,DOB.

def create_vendor_params
 params.require(:vendor).permit(
  :vendor_id, :quote_type, :first_name, :last_name, :email, 
  :phone_type,:phone, :request_type, :address1, :address_city, 
  :address_state,:address_zip, :DOB)
end
 "vendor": {
    "vendor_id": 2,
    "quote_type": "renter",
    "first_name":"Tony",
    "last_name":"Stark",
    "email":"tony@starkindustries",
    "phone_type":"mobile",
    "phone":"6504881234",
    "request_type":"renter",
    "address1":"123 Main Street",
    "address_city":"Palo Alto",
    "address_state":"CA",
    "address_zip":"94301",
    "DOB":"1990-07-22"
  }
def create 
 @vendor = Vendor.new(create_vendor_params)
 if @vendor.save
    render json: {'Success' }
 else
    render json: {error: @vendor.errors}
 end
end