0

Hi I am using the devise gem for rails for app.

My question is after user creates the account I want him/her to create a profile as well. I want both to happen at the same time so I can do a 1-1 match with the user id and profile id.

How can I do this?

Much thanks!

SuperString
  • 21,593
  • 37
  • 91
  • 122

1 Answers1

1

Make it so the user model has a has_one relationship with a profile model, and give the user model an after_create callback which creates a profile. What exactly are you confused about?

Batkins
  • 5,547
  • 1
  • 28
  • 27
  • what if the user signs in but dont create a profile? That will destroy my model because I want to do a 1-1 with the ids. – SuperString Feb 01 '12 at 22:25
  • how do you give the user model a after_create call back? I think devise hides the model and controller implementations? – SuperString Feb 01 '12 at 22:25
  • Use a [callback](http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html) to make the profile creation happen behind the scenes. Did you want a custom sign up screen so they have to enter in attributes for the profile? I think Devise allows you to do that if you want. – Batkins Feb 01 '12 at 22:27
  • [Here is a link to a question where someone has described how to override the registration and sign_in screen for Devise](http://stackoverflow.com/questions/3546289/override-devise-registrations-controller). – Batkins Feb 01 '12 at 22:33
  • Yes I want to do that. However, how do I generate the controllers for devise classes? – SuperString Feb 01 '12 at 22:59
  • Well [looking at what one of the users who answered has done in the last link I provided](http://stackoverflow.com/a/4527049/995874), you may not even need to generate a new controller. It looks like you can create your own custom registration form at `app/views/devise/registrations/new.html.erb`, and then it will use that for the registration form when creating, and you can specify your custom fields on the user model, and put the fields for it on this form. – Batkins Feb 02 '12 at 15:10