Reputation: 1013
I'm having some trouble figuring out how to automatically create a user profile object as soon as a user has signed up using devise. My profile object has been scaffolded out, now its a case of adding the empty Profile.new object to the user object. The following code is from registrations_controller.rb
protected
# If you have extra params to permit, append them to the sanitizer.
def configure_sign_up_params
devise_parameter_sanitizer.for(:sign_up) << :profile_id
end
# The path used after sign up.
def after_sign_up_path_for(resource)
p "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
resource.profile << Profile.new
p resource
super(resource)
end
What happens is that in the Rails console, the p '$$$$..' line is visible and there are no errors. However, in psql when looking at the profile_id is null. I've been scratching my head for hours, is this even the best way to do this, by passing it in the Devise's santizer?
We did think to instead redirect to the create_profile view that was scaffolded out, but in the case where a user signs up and then doesn't complete the profile, we'd have a problem. Does anyone have any advice on how to proceed, or how to pass the new Profile object in?
Upvotes: 1
Views: 136
Reputation: 228
Why are you adding a Profile.new
to the User
's profile
attribute (assuming the resource
model is a User
)?
Assuming you only have one Profile
per User
:
resource.profile = Profile.new
resource.save # you need to save the resource as well
super(resource)
...is what you want.
However, as pointed out by Sylvain, you actually want to handle this from the User
model itself, using the after_create
filter: attach the profile as soon as the User is created. No need to modify controller code.
Upvotes: 1
Reputation: 732
You might try to add a method in the User Model
after_create :create_profile
private
def create_profile
self.profiles.build(#something)
#according there is a link between your profiles object and user
end
Hope this helps
Upvotes: 1