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.

On Monday, December 30, 2013 12:03:57 PM UTC, Ruby-Forum.com User wrote:

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

On 30 December 2013 12:36, Ravi V. [email protected] wrote:

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

Frederick C. wrote in post #1131847:

On Monday, December 30, 2013 12:03:57 PM UTC, Ruby-Forum.com User wrote:

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
Ruby on Rails Guides: Ruby on Rails 2.3 Release Notes
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

Colin L. wrote in post #1131851:

On 30 December 2013 12:36, Ravi V. [email protected] wrote:

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

On Monday, December 30, 2013 5:12:27 PM UTC, Ruby-Forum.com User wrote:

Colin L. 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

Frederick C. wrote in post #1131871:

On Monday, December 30, 2013 5:12:27 PM UTC, Ruby-Forum.com User wrote:

Colin L. 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,

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

On Monday, December 30, 2013 9:11:16 PM UTC, Ruby-Forum.com User wrote:

purpose I added firstname & lastname like this

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