How to add a callback to create an account for the registered user.
Devise files (registrations_controller.rb) are under controllers/devise My user model has has_many :accounts relationship (and the account model has belongs_to :user)
First I don't know where to add the callback (what file?)
Then, how to automatically create a new account with the right user_id of the registered user?
Thanks in advance.
You can override devise's registration controller, add callback to create account using filters. Remember to name the file registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
after_filter :add_account
protected
def add_account
if resource.persisted? # user is created successfuly
resource.accounts.create(attributes_for_account)
end
end
end
then in your routes.rb tell devise to use overrided controller for registration
devise_for :users, controllers: { registrations: 'registrations'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With