combining models

I am new to ruby on rails.
I have 2 models, user and user_profile.
user has_one user_profile.
User_profile belongs_to user.
when creating user I want to create user_profile, in one form get user
and user_profile and save both.How ca I do that.
Thanks for your help.

I am new to ruby on rails.

I have 2 models, user and user_profile.

user has_one user_profile.

User_profile belongs_to user.

when creating user I want to create user_profile, in one form get user

and user_profile and save both.How ca I do that.

Thanks for your help.

One approach is to use accepts_nested_attributes - see http://guides.rubyonrails.org/v3.2.14/2_3_release_notes.html#nested-object-forms for example.

Fred

Frederick Cheung wrote in post #1131847:

I am new to ruby on rails.
I have 2 models, user and user_profile.
user has_one user_profile.
User_profile belongs_to user.
when creating user I want to create user_profile, in one form get user
and user_profile and save both.How ca I do that.
Thanks for your help.

One approach is to use accepts_nested_attributes -
see
http://guides.rubyonrails.org/v3.2.14/2_3_release_notes.html#nested-object-forms
for example.

Fred

Thanks Fred,

I had tried. This works, but while saving user_profile field values are
nil except ids.
my create method like this

@user = User.new(user_params)
@user_profile = @user.build_user_profile(params[:user_profile])

Thanks

Have a look in log/development.log and you will see the parameters you
are posting and check they are ok. Also you can do thinks like
inserting puts statements into your code, so if you insert

puts inspect params[:user_profile]

in the code above it will print the params in the server terminal
window so you can check they are ok.

Colin

Colin Law wrote in post #1131851:

I had tried. This works, but while saving user_profile field values are
nil except ids.
my create method like this

@user = User.new(user_params)
@user_profile = @user.build_user_profile(params[:user_profile])

Have a look in log/development.log and you will see the parameters you
are posting and check they are ok. Also you can do thinks like
inserting puts statements into your code, so if you insert

puts inspect params[:user_profile]

in the code above it will print the params in the server terminal
window so you can check they are ok.

Colin

I found the reason but I can not solve it.
user_profile is unpermitted parameter.

I added

def user_params
      params.require(:user).permit(:email, :password,
:user_profile_attributes =>[])
    end
in the conroller and

accepts_nested_attributes_for :user_profile

in the model.

still the same problem.

Thanks

Colin Law wrote in post #1131851:

>> @user = User.new(user_params)

>> @user_profile = @user.build_user_profile(params[:user_profile])

>

> Have a look in log/development.log and you will see the parameters you

> are posting and check they are ok. Also you can do thinks like

> inserting puts statements into your code, so if you insert

I found the reason but I can not solve it.

user_profile is unpermitted parameter.

I added

def user_params

      params.require(:user).permit(:email, :password,

:user_profile_attributes =>[])

    end

You need to add the attributes that are allowed for user_profile.

Fred

Frederick Cheung wrote in post #1131871:

Colin Law wrote in post #1131851:

> inserting puts statements into your code, so if you insert

def user_params

      params.require(:user).permit(:email, :password,

:user_profile_attributes =>[])

    end

You need to add the attributes that are allowed for user_profile.

Fred

in fact i want to add all attributes of user_profile. for testing
purpose I added firstname & lastname like this
def user_params
      params.require(:user).permit(:email, :password,
:user_profile_attributes => [:firstname, :lastname])
    end

but still not working.
user_profile is in unpermitted parameter.

Unpermitted parameters: password_confirmation, user_profile

Thanks,

>

> You need to add the attributes that are allowed for user_profile.

in fact i want to add all attributes of user_profile. for testing

purpose I added firstname & lastname like this

def user_params

      params.require(:user).permit(:email, :password,

:user_profile_attributes => [:firstname, :lastname])

    end

but still not working.

user_profile is in unpermitted parameter.

There shouldn't really be any parameters of that name. It's not clear what changes you made after I suggested accepts_nested_attributes. In particular how you call fields_for in the view is very relevant.

Fred

Hi Ravi,
Did you get solution ?
Thanks,
Manoj Menon
RoR Developer
Maxxion Systems.