I’m using devise_invitable with devise in my Rails 3 app. I want to give my users the ability to invite other users and group those invited users in advance of the invitees signing up.
My problem is that once the invitee comes along and signs up (but doesn’t use the invite URL), the destroy_if_previously_invited around_filter comes along, destroys the original user record and recreates a new record for the user, retaining the invitation data but not transferring the user_groups records along with it.
I’d like to simply override this around_filter by doing a search for any user_groups that match the originally invited user_id and saving them with the newly created user_id.
I keep getting the error:
LocalJumpError in Users::RegistrationsController#create
no block given (yield)
My route looks like this:
devise_for :users, :controllers => { :registrations => "users/registrations" }
I’ve set this as the override in app/controllers/users/registrations_controller.rb:
class Users::RegistrationsController < Devise::RegistrationsController
around_filter :destroy_if_previously_invited, :only => :create
private
def destroy_if_previously_invited
invitation_info = {}
user_hash = params[:user]
if user_hash && user_hash[:email]
@user = User.find_by_email_and_encrypted_password(user_hash[:email], '')
if @user
invitation_info[:invitation_sent_at] = @user[:invitation_sent_at]
invitation_info[:invited_by_id] = @user[:invited_by_id]
invitation_info[:invited_by_type] = @user[:invited_by_type]
invitation_info[:user_id] = @user[:id]
@user.destroy
end
end
# execute the action (create)
yield
# Note that the after_filter is executed at THIS position !
# Restore info about the last invitation (for later reference)
# Reset the invitation_info only, if invited_by_id is still nil at this stage:
@user = User.find_by_email_and_invited_by_id(user_hash[:email], nil)
if @user
@user[:invitation_sent_at] = invitation_info[:invitation_sent_at]
@user[:invited_by_id] = invitation_info[:invited_by_id]
@user[:invited_by_type] = invitation_info[:invited_by_type]
user_groups = UserGroup.find_all_by_user_id(invitation_info[:user_id])
for user_group in user_groups do
user_group.user_id = @user.id
user_group.save!
end
@user.save!
end
end
end
I may also just be going about this all wrong. Any ideas would be appreciated.
First, there is no need to set the around_filter again, as devise_invitable already sets this. All you need to do is redefine the methods in your UsersController and those will be called instead of the devise_invitable ones.
Second, it looks like you are combining the two methods when they should remain seperate and overridden (I am basing this off latest version of devise_invitatable 1.1.1)
Try something like this: