0

我在我的 Rails 3 应用程序中使用devise_invitablewith 。devise我想让我的用户能够在受邀者注册之前邀请其他用户并将这些受邀用户分组。

我的问题是,一旦受邀者出现并注册(但不使用邀请 URL),destroy_if_previously_invitedaround_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

我也可能只是把这一切都错了。任何想法,将不胜感激。

4

1 回答 1

0

首先,不需要再次设置 around_filter,因为 devise_invitable 已经设置了它。您需要做的就是重新定义 UsersController 中的方法,这些方法将被调用,而不是 devise_invitable 方法。

其次,当这两种方法应该保持独立和被覆盖时,您似乎正在组合它们(我基于最新版本的 devise_invitatable 1.1.1)

尝试这样的事情:

class Users::RegistrationsController < Devise::RegistrationsController
  protected

  def destroy_if_previously_invited
    hash = params[resource_name]
    if hash && hash[:email]
      resource = resource_class.where(:email => hash[:email], :encrypted_password => '').first
      if resource
        @old_id = resource.id #saving the old id for use in reset_invitation_info
        @invitation_info = Hash[resource.invitation_fields.map {|field|
          [field, resource.send(field)]
        }]
        resource.destroy
      end
    end
  end

  def reset_invitation_info
    # Restore info about the last invitation (for later reference)
    # Reset the invitation_info only, if invited_by_id is still nil at this stage:
    resource = resource_class.where(:email => params[resource_name][:email], :invited_by_id => nil).first
    if resource && @invitation_info
      resource.invitation_fields.each do |field|
        resource.send("#{field}=", @invitation_info[field])
      end
      ### Adding your code here
      if @old_id
        user_groups = UserGroup.find_all_by_user_id(@old_id)
        for user_group in user_groups do
          user_group.user_id = resource.id
          user_group.save!
        end
      end
      ### End of your code
      resource.save!
    end
  end
于 2012-10-22T01:21:38.413 回答