Rails 5 Api create new object from json with nested resource
up vote
0
down vote
favorite
this is the json received as parameters from external angular webapp:
{
"provincia": {
"id": 1,
"name": "Province"
},
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
this is my controller
def create
@seller = Seller.new(seller_params)
if @seller.save
render json: @seller, status: :created, location: @seller
else
puts @seller.errors.full_messages
render json: @seller.errors, status: :unprocessable_entity
end
end
this is seller_params
def seller_params
params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
end
models: Seller belongs_to Provincia
server console output error full message
Provincia must exist
Which modification in the Rails API should I do to make it work, and save the new seller? thanks in advance.
ruby-on-rails ruby angular api ruby-on-rails-5
add a comment |
up vote
0
down vote
favorite
this is the json received as parameters from external angular webapp:
{
"provincia": {
"id": 1,
"name": "Province"
},
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
this is my controller
def create
@seller = Seller.new(seller_params)
if @seller.save
render json: @seller, status: :created, location: @seller
else
puts @seller.errors.full_messages
render json: @seller.errors, status: :unprocessable_entity
end
end
this is seller_params
def seller_params
params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
end
models: Seller belongs_to Provincia
server console output error full message
Provincia must exist
Which modification in the Rails API should I do to make it work, and save the new seller? thanks in advance.
ruby-on-rails ruby angular api ruby-on-rails-5
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
this is the json received as parameters from external angular webapp:
{
"provincia": {
"id": 1,
"name": "Province"
},
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
this is my controller
def create
@seller = Seller.new(seller_params)
if @seller.save
render json: @seller, status: :created, location: @seller
else
puts @seller.errors.full_messages
render json: @seller.errors, status: :unprocessable_entity
end
end
this is seller_params
def seller_params
params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
end
models: Seller belongs_to Provincia
server console output error full message
Provincia must exist
Which modification in the Rails API should I do to make it work, and save the new seller? thanks in advance.
ruby-on-rails ruby angular api ruby-on-rails-5
this is the json received as parameters from external angular webapp:
{
"provincia": {
"id": 1,
"name": "Province"
},
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
this is my controller
def create
@seller = Seller.new(seller_params)
if @seller.save
render json: @seller, status: :created, location: @seller
else
puts @seller.errors.full_messages
render json: @seller.errors, status: :unprocessable_entity
end
end
this is seller_params
def seller_params
params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
end
models: Seller belongs_to Provincia
server console output error full message
Provincia must exist
Which modification in the Rails API should I do to make it work, and save the new seller? thanks in advance.
ruby-on-rails ruby angular api ruby-on-rails-5
ruby-on-rails ruby angular api ruby-on-rails-5
edited 1 hour ago
asked 1 hour ago
matQ
1149
1149
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
The way that you are permiting your params in the controller are not correct:
Rails Docs
You need to pass your provincia_id in your attributes or permit that params that are you passing by
Way 1:
{
"provincia_attributes": {
"id": 1,
"name": "Province"
},
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
SellersController.rb
def seller_params
params.require(:seller).permit(:username, :direccion, :email, provincia_attributes: [:id, :name])
end
Way 2
{
"provincia_id": "1"
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
SellersController.rb
def seller_params
params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
end
New contributor
add a comment |
up vote
0
down vote
Try changing the params like this:
def seller_params
p = params.require(:seller).premit(:username, :direccion, :email).to_h
p[:seller][:provincia_id] = params[:seller][:provincia][:id]
p
end
That will add the "provincia_id" key you are missing for the association. I call to_h to get a new hash because I don't like to mutate the original params and you have already permited the values you want so that Hash is safe to use.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
The way that you are permiting your params in the controller are not correct:
Rails Docs
You need to pass your provincia_id in your attributes or permit that params that are you passing by
Way 1:
{
"provincia_attributes": {
"id": 1,
"name": "Province"
},
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
SellersController.rb
def seller_params
params.require(:seller).permit(:username, :direccion, :email, provincia_attributes: [:id, :name])
end
Way 2
{
"provincia_id": "1"
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
SellersController.rb
def seller_params
params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
end
New contributor
add a comment |
up vote
1
down vote
The way that you are permiting your params in the controller are not correct:
Rails Docs
You need to pass your provincia_id in your attributes or permit that params that are you passing by
Way 1:
{
"provincia_attributes": {
"id": 1,
"name": "Province"
},
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
SellersController.rb
def seller_params
params.require(:seller).permit(:username, :direccion, :email, provincia_attributes: [:id, :name])
end
Way 2
{
"provincia_id": "1"
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
SellersController.rb
def seller_params
params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
end
New contributor
add a comment |
up vote
1
down vote
up vote
1
down vote
The way that you are permiting your params in the controller are not correct:
Rails Docs
You need to pass your provincia_id in your attributes or permit that params that are you passing by
Way 1:
{
"provincia_attributes": {
"id": 1,
"name": "Province"
},
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
SellersController.rb
def seller_params
params.require(:seller).permit(:username, :direccion, :email, provincia_attributes: [:id, :name])
end
Way 2
{
"provincia_id": "1"
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
SellersController.rb
def seller_params
params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
end
New contributor
The way that you are permiting your params in the controller are not correct:
Rails Docs
You need to pass your provincia_id in your attributes or permit that params that are you passing by
Way 1:
{
"provincia_attributes": {
"id": 1,
"name": "Province"
},
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
SellersController.rb
def seller_params
params.require(:seller).permit(:username, :direccion, :email, provincia_attributes: [:id, :name])
end
Way 2
{
"provincia_id": "1"
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}
SellersController.rb
def seller_params
params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
end
New contributor
New contributor
answered 39 mins ago
Leonardo da Rosa
112
112
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
Try changing the params like this:
def seller_params
p = params.require(:seller).premit(:username, :direccion, :email).to_h
p[:seller][:provincia_id] = params[:seller][:provincia][:id]
p
end
That will add the "provincia_id" key you are missing for the association. I call to_h to get a new hash because I don't like to mutate the original params and you have already permited the values you want so that Hash is safe to use.
add a comment |
up vote
0
down vote
Try changing the params like this:
def seller_params
p = params.require(:seller).premit(:username, :direccion, :email).to_h
p[:seller][:provincia_id] = params[:seller][:provincia][:id]
p
end
That will add the "provincia_id" key you are missing for the association. I call to_h to get a new hash because I don't like to mutate the original params and you have already permited the values you want so that Hash is safe to use.
add a comment |
up vote
0
down vote
up vote
0
down vote
Try changing the params like this:
def seller_params
p = params.require(:seller).premit(:username, :direccion, :email).to_h
p[:seller][:provincia_id] = params[:seller][:provincia][:id]
p
end
That will add the "provincia_id" key you are missing for the association. I call to_h to get a new hash because I don't like to mutate the original params and you have already permited the values you want so that Hash is safe to use.
Try changing the params like this:
def seller_params
p = params.require(:seller).premit(:username, :direccion, :email).to_h
p[:seller][:provincia_id] = params[:seller][:provincia][:id]
p
end
That will add the "provincia_id" key you are missing for the association. I call to_h to get a new hash because I don't like to mutate the original params and you have already permited the values you want so that Hash is safe to use.
answered 40 mins ago
arieljuod
5,63911121
5,63911121
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343652%2frails-5-api-create-new-object-from-json-with-nested-resource%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown