在 Rails 上 Ruby 中使用用户控制器中的当前记录设计 super 发送邮件
Devise super do with current record in user controller for sending mails in Ruby on Rails
我正在尝试通过电子邮件为用户设置注册确认。我正在使用设计进行身份验证。但是我无法从设计控制器访问保存的用户资源,尽管我徒劳地尝试了一些修补程序。如果有人可以提供帮助,那就太好了!!!
我正在尝试在用户保存后向注册用户发送确认 link。但是我无法像设计控制器中的任何常用实例变量一样获取新用户记录。
现在我的用户注册控制器看起来像这样:
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
有人知道从设计控制器发送电子邮件确认电子邮件吗?提前致谢!!!
在使用 devise 的同时,我们可以通过多种方式解决 super do 要求,例如
在用户模型中添加 after_create
操作以发送确认电子邮件。
但我认为这个解决方案很方便,因为您确实需要与 super 一起使用设计注册控制器执行您的操作。
资源保存后添加代码
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
确保该设计没有隐含双重渲染或多重重定向
我正在尝试通过电子邮件为用户设置注册确认。我正在使用设计进行身份验证。但是我无法从设计控制器访问保存的用户资源,尽管我徒劳地尝试了一些修补程序。如果有人可以提供帮助,那就太好了!!!
我正在尝试在用户保存后向注册用户发送确认 link。但是我无法像设计控制器中的任何常用实例变量一样获取新用户记录。
现在我的用户注册控制器看起来像这样:
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
有人知道从设计控制器发送电子邮件确认电子邮件吗?提前致谢!!!
在使用 devise 的同时,我们可以通过多种方式解决 super do 要求,例如
在用户模型中添加 after_create
操作以发送确认电子邮件。
但我认为这个解决方案很方便,因为您确实需要与 super 一起使用设计注册控制器执行您的操作。
资源保存后添加代码
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
确保该设计没有隐含双重渲染或多重重定向