我在我的 Rails 3 应用程序中使用devise_invitable
with 。devise
我想让我的用户能够在受邀者注册之前邀请其他用户并将这些受邀用户分组。
我的问题是,一旦受邀者出现并注册(但不使用邀请 URL),destroy_if_previously_invited
around_filter 就会出现,破坏原始用户记录并为用户重新创建新记录,保留邀请数据但不传输user_groups 记录随之而来。
我想通过搜索与最初邀请的 user_id 匹配的任何 user_groups 并使用新创建的 user_id 保存它们来简单地覆盖此 around_filter。
我不断收到错误:
LocalJumpError in Users::RegistrationsController#create
no block given (yield)
我的路线如下所示:
devise_for :users, :controllers => { :registrations => "users/registrations" }
我已在 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
我也可能只是把这一切都错了。任何想法,将不胜感激。