I've tried it in a number of ways but it does not work. I have in this project, another controller with the same code working, the difference that another uses the has_many relationship, and the one with problem is has_one and belongs_to. Let's go to the code.
class Endereco < ActiveRecord::Base
has_one :user
end
class User < ActiveRecord::Base
belongs_to :endereco
accepts_nested_attributes_for :endereco
The strong parameter in controller:
class Backoffice::UsersController < BackofficeController
def create
@user = User.new(params_user)
respond_to do |format|
if @user.save!
format.json { render json: @user, include: :endereco}
else
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def params_user
params.require(:user).permit(:nome,
:email,
:password,
:password_confirmation,
:cpf,
:tel_fixo,
:tel_cel,
endereco_attributes: [
:rua,
:bairro,
:cidade,
:uf,
:cep,
:referencia,
:numero,
:complemento
])
end
When I send this json via POSTMAN I get the error> undefined method `with_indifferent_access' for #Array: on this line
@user = User.new(params_user)
{ "user":{
"nome" : "teste1",
"email" : "teste@tt.com",
"password": "123",
"password_confirmation": "123",
"cpf": "123321",
"tel_fixo": "123321",
"tel_cel": "asdsd",
"endereco_attributes": [
{
"rua": "tt",
"bairro": "tete",
"cidade": "asdas",
"uf":"asdasd",
"cep": "12321",
"referencia": "asdasd",
"numero":"123",
"complemento":"123" }
]
}}
Someone to give me a light? Thank you!