のための `with_indifferent_access '私はさまざまな方法で試しましたが、うまくいきません。私はこのプロジェクトで同じコードが動作している別のコントローラを持っていますが、別のコントローラはhas_many関係を使い、問題のあるコントローラはhas_oneとbelongs_toです。コードに行きましょう。未定義のメソッド#for <配列:
class Endereco < ActiveRecord::Base
has_one :user
end
class User < ActiveRecord::Base
belongs_to :endereco
accepts_nested_attributes_for :endereco
コントローラに強いパラメータ:私はPOSTMANを経由して、このJSONを送信すると
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
私が取得エラー#Array用>未定義のメソッド `with_indifferent_access':この行の
@user = User.new(params_user)
{ "user":{
"nome" : "teste1",
"email" : "[email protected]",
"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" }
]
}}
誰かが私に光を与えますか?ありがとうございました!
ユーザーはjsonを介してのみ登録できますが、私はまだよく理解していません。endereco_attributesをenderecoに変更しました。今回は、Endereco(#42380200)の期待通りにArray(#18448440) –