ruby on rails - How can I add this transaction to when after email confirmation is completed with Devise? -
assuming existing user(id:1367) can send url friends invitation.
http://example.com/users/sign_up?invitation=1367
then users table has hidden column called invitation
.
and procedure how works want.
his friend input info sign up.
he hits submit button hidden field 'invitation' sent form.
1367
set in column called 'invitation' of record.
he receive confirmation mail, , when clicks on link, i'd add together transaction, , execute 1 time account. of course, shouldn't executed when existing user tried re-activate. first confirmation new user.
code
@user = user.find_by_invitation(current_user.invitation) @user.friends = @user.friends + 1 @user.save
i have registration controller helps adding transaction devise. want know how can implement procedure app.
should this?
registrations_controller.rb
def after_???????? if @user = user.find_by_invitation(current_user.invitation) @user.friends = @user.friends + 1 @user.save end end
you can on user model. utilize callback called after_create
triggered after user created.
# user.rb after_create :handle_invitation_code private def handle_invitation_code # invitation here # i'm assuming want credit # whoever invited user # assuming invitation contains # id of referrer if referrer = user.find_by_id(invitation) # referrer end end
be warned if homecoming false on callback, cause rollback , record won't saved.
update: callback after confirmation
instead of using after_create
, utilize before_save
if
option
before_save :handle_invitation_code, if: :just_confirmed? def just_confirmed? confirmed_at_changed? && confirmed_at_was.nil? end def handle_invitation_code ... end
ruby-on-rails ruby-on-rails-3 devise
No comments:
Post a Comment