Linking two models in a multi-model form

前端 未结 1 1132
星月不相逢
星月不相逢 2021-01-07 11:54

I have a nested multimodel form right now, using Users and Profiles.

Users has_one profile, and Profile belongs_to Users.

When the form is submitted, a new u

1条回答
  •  孤街浪徒
    2021-01-07 12:25

    To answer question number one, change the following:

    @profile = Profile.new(params[:profile])
    

    to

    @profile = @user.profile.build(params[:profile]) #In the case of a has_many relationship
    

    or

    @profile = @user.build_profile(params[:profile]) #In the case of a has_one relationship
    

    The build command builds a new profile with the user_id properly set.

    For the second question, can you delete the query for Role and Company during the new action and instead assign those during the create action? This would remove the necessity of passing hidden parameters.

    0 讨论(0)
提交回复
热议问题