0

我正在尝试通过电子邮件为用户设置注册确认。我正在使用设计进行身份验证。但是我无法从设计控制器访问保存的用户资源,尽管我尝试了一些无用的修补程序。如果有人可以提供帮助,那就太好了!!!

我正在尝试在用户保存后向注册用户发送确认链接。但是我无法将新用户记录作为设计控制器中的任何常用实例变量来获取。

现在我的用户注册控制器看起来像这样:

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :select_plan, only: :new
  # Extend the default Devise gem behaviour so that 
  # the users signing up with a Pro account(plan_id 2) should be
  # saved with a Stripe subscription function
  # Otherwise, save the sign up as usual.
  def create
    super do |resource|
      # @user = User.new(configure_permitted_parameters)
      if params[:plan]
        resource.plan_id = params[:plan]
        if resource.plan_id == 2
          resource.save_with_subscription
        else
          resource.save
        end
        //These do not works and returns null
        //@user = User.find_by_email(params[:email])
        //@user = User.find(params[:id]
        //@user = resource
        UserMailer.registration_confirmation(params[:email]).deliver
        flash[:success] = "Please confirm your email address to continue"
        redirect_to root_url
      end
    end
  end
  
  private
    def select_plan
      unless (params[:plan] == '1' || params[:plan] == '2')
        flash[:notice] = "Please select a membership plan to sign up."
        redirect_to root_url
      end
    end
end

用户型号:

class User < ApplicationRecord
  
  before_create :confirmation_token
  
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
         
  belongs_to :plan
  has_one :profile
  
  attr_accessor :stripe_card_token
  
  # If Pro user passes the validation for email, password etc. then call Stripe
  # and tell Stripe to add a subscription by charging customer's card
  # Stripe then returns a customer token in response 
  # Store the token id as customer id and save the user
  def save_with_subscription
    if valid?
      customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
      self.stripe_customer_token = customer.id
      save!
    end
  end

  private
    
    def confirmation_token
      if self.confirm_token.blank?
          self.confirm_token = SecureRandom.urlsafe_base64.to_s
      end
    end
  
  def email_activate
    self.email_confirmed = true
    self.confirm_token = nil
    save!(:validate => false)
  end
  
end

任何人都知道从设计控制器发送电子邮件确认电子邮件吗?提前致谢!!!

4

1 回答 1

0

在使用 devise 时,我们可以通过多种方式解决超级执行需求,例如after_create在 User 模型中添加操作以发送确认电子邮件。但我认为这个解决方案很方便,因为你真的需要与超级用户一起使用设计注册控制器执行你的操作。在资源保存后添加代码。

class Users::RegistrationsController < Devise::RegistrationsController

  def create
    super do |resource|

   # Your super codes here
    resource.save

    yield resource if block_given?
    if resource.persisted?

     # We know that the user has been persisted to the database, so now we can send our mail to the resource(user) now!
     UserMailer.registration_confirmation(resource).deliver
     # .... 
    end
    # ....
    end
   end
 # .....
end

确保该设计没有暗示双重渲染或多重重定向

于 2020-07-30T09:26:19.367 回答