【发布时间】:2017-01-24 12:18:18
【问题描述】:
我要填充的 JSON 响应如下:
{
"vendor":
{
"name": "Mozelle Luettgen MD",
"email": "tyqmn@example.net",
"phone_no": "9999997660",
"addressline1": "Kulas Stravenue",
"addressline2": "64636 Lynch Springs",
"landmark": "Apt. 142",
"city": "South Hannaview",
"state": "North Dakota",
"country": "Palau",
"pincode": "53912-6122",
"latitude": 50.8247548421224,
"longitude": -81.8429583068792,
"status": true
}
}
我用于创建供应商的控制器代码是
def create
@vendor = Vendor.new(vendor_params)
respond_to do |format|
if @vendor.save
format.html { redirect_to @vendor, notice: 'Vendor was
successfully created.' }
format.json { render :show, status: :created, location: @vendor,
:msg => { :status => "ok" , :result => @vendor.json, :message =>
"Succesfully Created" }
}
else
format.html { render :new }
format.json { render json: @vendor.errors, status:
:unprocessable_entity,
:msg =>
{ :status => "Error", :message => "Unprocessable Entity" }
}
end
end
end
def vendor_params
params.require(:vendor).permit(:name, :email, :phone_no,
:addressline1, :addressline2, :landmark,
:city, :state, :country, :pincode, :latitude, :longitude, :status,
{products_attributes: [:id, :product_name, :price]},
{vendor_products_attributes: [:id, :vendor_product_id, :vendor_id,
:product_id, :copies, :_destroy]})
end
在运行此链接http://localhost:3000/vendors/create_vendor 时,状态显示 200 ok,但是当我查找 JSON 响应时,对于创建的供应商,它会抛出 Unexpected '
【问题讨论】:
-
您是否使用调试器检查过控制器上的哪些参数?
-
我已经更新了显示允许的参数的问题。
-
我认为@power 要求调试器查看当您在 Postman 中运行事务时控制器中实际存在哪些参数。
-
哦,对@Power ...很好地检查 binding.pry 上的参数,我得到以下参数:{"vendor"=> {"name"=>"Mozelle Luettgen MD", "email "=>"ajczzz@example.net", "phone_no"=>"9999997660", "addressline1"=>"Kulas Stravenue", "addressline2"=>"64636 Lynch Springs", "landmark"=>"Apt. 142 ", "city"=>"South Hannaview", "state"=>"North Dakota", "country"=>"Palau", "pincode"=>"53912-6122", "latitude"=>50.8247548421224, "经度”=>-81.8429583068792, “状态”=>true}, “控制器”=>“供应商”, “动作”=>“创建”}。这是你想要的吗?
-
如果您编辑 JSON 返回结构并将
location: @vendor更改为:location => @vendor.id,会发生什么?
标签: ruby-on-rails json postman