combining models

39 views
Skip to first unread message

Ravi Vendan

unread,
Dec 30, 2013, 7:03:57 AM12/30/13
to rubyonra...@googlegroups.com
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.

--
Posted via http://www.ruby-forum.com/.

Frederick Cheung

unread,
Dec 30, 2013, 7:27:19 AM12/30/13
to rubyonra...@googlegroups.com


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

Ravi Vendan

unread,
Dec 30, 2013, 7:36:42 AM12/30/13
to rubyonra...@googlegroups.com
Frederick Cheung wrote in post #1131847:
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 Law

unread,
Dec 30, 2013, 9:04:22 AM12/30/13
to rubyonra...@googlegroups.com
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

Ravi Vendan

unread,
Dec 30, 2013, 12:12:27 PM12/30/13
to rubyonra...@googlegroups.com
Colin Law wrote in post #1131851:
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.

Frederick Cheung

unread,
Dec 30, 2013, 3:34:24 PM12/30/13
to rubyonra...@googlegroups.com
On Monday, December 30, 2013 5:12:27 PM UTC, Ruby-Forum.com User wrote:
> 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

Ravi Vendan

unread,
Dec 30, 2013, 4:11:16 PM12/30/13
to rubyonra...@googlegroups.com
Frederick Cheung wrote in post #1131871:
> On Monday, December 30, 2013 5:12:27 PM UTC, Ruby-Forum.com User wrote:
>> 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,

Frederick Cheung

unread,
Dec 30, 2013, 5:44:45 PM12/30/13
to rubyonra...@googlegroups.com
On Monday, December 30, 2013 9:11:16 PM UTC, Ruby-Forum.com User wrote:

>
> >
>
> > 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

Manoj M.

unread,
Jan 3, 2014, 4:20:05 AM1/3/14
to rubyonra...@googlegroups.com
Hi Ravi,
Did you get solution ?
Thanks,
Manoj Menon
RoR Developer
Maxxion Systems.
Reply all
Reply to author
Forward
0 new messages