[英]Ruby JSON API and post request with Postman
I'm using postman to make post request to my rails/grape API 我正在使用邮递员向我的rails / grape API发出发布请求
Here is the json object 这是json对象
{
"customer":{
"first_name":"Joe",
"last_name":"Doe",
"email":"test@test.com",
"phone":"999-999-9999",
"addresses_attributes":[{
"address":{
"address1":"123 somewhere st",
"customer_id":"",
"address2":"",
"city":"Moldor",
"state":"CA",
"zip":"99999",
"region_id":"1"
}
}]
}
}
If send this to a api/v1/customers.json I get the following error message 如果将其发送到api / v1 / customers.json我会收到以下错误消息
"error": "first_name is missing, last_name is missing, email is missing, phone is missing",
If I change the JSON format to: 如果我将JSON格式更改为:
{
"first_name":"Joe",
"last_name":"Doe",
"email":"test@test.com",
"phone":"999-999-9999",
"addresses_attributes":[{
"address":{
"address1":"123 somewhere st",
"customer_id":"",
"address2":"",
"city":"Moldor",
"state":"CA",
"zip":"99999",
"region_id":"1"
}
}]
}
I does creates the customer but it does not creates the address 我确实创建了客户,但没有创建地址
In my Models I have 在我的模特儿中
#customer.br
has_many :addresses
accepts_nested_attributes_for :addresses, allow_destroy: true
#address.rb
belongs_to :customer
Here is the api/vi/curtomer.rb 这是api / vi / curtomer.rb
desc "Create a Customer"
params do
requires :first_name, type: String, desc: "First Name"
requires :last_name, type: String, desc: "Last Name"
requires :email, type: String, desc: "Email"
requires :phone, type: String, desc: "Phone"
optional :notes, type: String, desc: "Notes"
end
post do
# Tried with new instead of create
# test = Customer.new({
# first_name: params[:first_name],
# last_name: params[:last_name],
# email: params[:email],
# phone: params[:phone],
# notes: params[:notes],
# addresses_attributes: params[:addresses]
# })
Customer.create({
first_name: params[:first_name],
last_name: params[:last_name],
email: params[:email],
phone: params[:phone],
notes: params[:notes]
# addresses_attributes: params[:addresses]
})
end
The address looks pretty much the same as the customers I'm not using controllers per Grapes Documentation 根据Grapes文档,该地址看起来与我未使用控制器的客户几乎相同
EDIT: to add customer.rb create code 编辑:添加customer.rb创建代码
Think your json data should be: 认为您的json数据应为:
{ "customer":
{
"first_name":"Joe",
"last_name":"Doe",
"email":"test@test.com",
"phone":"999-999-9999",
"addresses_attributes":[{
"address1":"123 somewhere st",
"customer_id":"",
"address2":"",
"city":"Moldor",
"state":"CA",
"zip":"99999",
"region_id":"1"
}]
}
}
Refer to this example . 请参考此示例 。
If it not works, show your controller code. 如果不起作用,请显示您的控制器代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.