【问题标题】:Devise asks for confirmation before accepting invitation设计在接受邀请之前要求确认
【发布时间】:2013-09-11 11:09:57
【问题描述】:

我正在我的 rails 应用程序中创建一个模块,用于“添加团队成员”到项目中。 我正在使用 devise_invitable。

如果我添加一个新的电子邮件地址,确认邮件和邀请邮件都将被发送......因为这更有意义

我只想发送邀请邮件(用户接受邀请邮件后可以发送到注册页面)……在接受邀请之前确认没有意义。

我目前的代码如下:

    def create_team_members
      # find the case
      @case = Case.find_by_id(params[:id])
      # TODO:: Will change this when add new cse functionality is done

      params[:invitation][:email].split(',').map(&:strip).each do |email|
      # create an invitation
      @invitation = Invitation.new(email: "#{email}".gsub(/\s+/, ''), role: params[:invitation][:role].rstrip, case_id: @case.id, user_type_id: params[:invitation][:user_type_id])

        if @invitation.save
          # For existing users fire the mail from user mailer
          if User.find_by_email(email).present?
            UserMailer.invite_member_instruction(@invitation).deliver
            @invitation.update_attributes(invited_by: current_user.id)

          else
            # For new users use devise invitable
            user = User.invite!(email: "#{email}", name: "#{email}".split('@').first.lstrip)
            user.skip_confirmation!
            user.save
            @invitation.update_attributes(invitation_token: user.invitation_token, invited_by: current_user.id)
          end
        end
      end
      redirect_to dashboard_path
    end

我不知道我做错了什么......

请帮助...谢谢。

【问题讨论】:

  • 仅看代码,很难判断。 params[:invitation][:email] 参数长什么样子?

标签: ruby-on-rails ruby authentication devise


【解决方案1】:

有一个变通方法,我们可以覆盖用户模型中的 devise_invitable。 目前你的用户模型看起来有点像这样

class User < ActiveRecord::Base
   devise :confirmable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :invitable
   # Rest of the code
end

设计方法send_confirmation_instructions负责发送确认邮件。通过覆盖它我们可以停止发送确认电子邮件并使用户同时确认。

改成这个

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :invitable

  # Overriding devise send_confirmation_instructions
  def send_confirmation_instructions
    super
    self.confirmation_token = nil    # clear's the confirmation_token
    self.confirmed_at = Time.now.utc # confirm's the user
    self.save
  end

  # Rest of the code
end

现在邮件不会发送了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多