Mongoidを少し使い慣れていますが、ActiveRecordでかなりの経験があります。私は、このフォームを送信するたびに、私は次のモデル受け入れネストした属性mongoidをhas_oneの関係に使用
def Company
field :name
has_one :owner, autosave: true, class_name: 'User', inverse_of: :company
accepts_nested_attributes_for :owner
end
def User
belongs_to :company, inverse_of: :owner
has_one :profile
end
マイRegistrationControllerは、以下の方法
def new
@company = Company.new
@company.build_owner
@company.owner.build_profile
respond_with @company
end
そして、私の見解ではありが...
= simple_form_for @company, url: user_registration_path do |f|
= f.error_notification
.inputs
= f.simple_fields_for @company.owner do |o|
= o.input :email, required: true, autofocus: true
= o.simple_fields_for @company.owner.profile do |p|
= p.input :first_name, required: true
= p.input :last_name, required: true
= f.input :name, label: 'Company Name'
= f.input :subdomain
= o.input :password, required: true
= o.input :password_confirmation, required: true
.actions
= f.button :submit, "Sign up"
、のparamsは、返されますその:
{"utf8"=>"✓",
"authenticity_token"=>"6z8+evYUwZwx3iADFewsMHiPl00vT7Eq6WaD8BOnQBc=",
"company"=>
{"user"=>
{"email"=>"[email protected]",
"profile"=>{"first_name"=>"testing", "last_name"=>"testing"},
"password"=>"testing",
"password_confirmation"=>"testing"},
"name"=>"testing",
"subdomain"=>"testing"},
"commit"=>"Sign up",
"action"=>"create",
"controller"=>"users/registrations"}
まず、ユーザー属性にキーがある理由を理解できません。user、user_attributesまたは:owner_attributes?モンゴイドのウェブサイトの例は、それを示唆しているようです。次に、company = Company.new(params [:company])を実行し、そのオブジェクトのcompany.ownerを実行すると、無名オブジェクトが取得されます。しかし、company.userを実行すると正しいユーザーオブジェクトが返されます。私は、paramsの中にキーがあるなら、所有者(ユーザの代わりに)であることが分かった。しかし、それはデフォルトでは起こっていません。たぶんシンプルフォームとは何か?どんな助けでも大歓迎です!
私は式からsimple_formを削除して、標準のform_for/fields_forメソッドを使用してみます。また、モデルが関連するオブジェクトに対してaccept_nested_fields_forを宣言していることを確認してください。あなたが会社に変更した場合、それはうまくいくのですか? – OzBandit
解決策を見つけましたか? – baj