iCyborg
iCyborg

Reputation: 4728

How to connect Profile Controller with User Controller?

I have USER through Devise and have created Profile through scaffolding, now everything is working fine but I am not able to "connect" the user with the profile.

This is what I have done so far -

user.rb

has_one :profile

profile.rb

belongs_to :user

I have created the user_id column through migration in profiles table too.

Now when I am logged in and fill the form /profiles/new, it creates the profile but it is NO way linked to the user as the user_id filed is NULL. Also the user is able to create multiple profiles where as I thought he can only create one as I have put :has_one relationship ?

Any Help ?

Edit - in profiles_controller file, I also tried to change

@user_profile = UserProfile.new(params[:user_profile])

to

@user_profile = current_user.userprofile.new(params[:user_profile])

but it is giving undefined method 'UserProfile'

Upvotes: 0

Views: 292

Answers (1)

John H
John H

Reputation: 2488

@user_profile = current_user.userprofile.new(params[:user_profile])

Should be

@user_profile = current_user.build_user_profile(params[:user_profile])

Upvotes: 2

Related Questions