[英]Rails 4 - Devise - Confirmable subject to approval
我正在尝试学习如何阅读rubydocs。 我敢肯定,在尝试使用这些文档时我会错过一些基本的基本知识,但就目前而言,我很沮丧,无法向我提供信息,但没有附带任何形式的可理解的用户指南。
我想修改我的设计,以便用户可以提名他们想要加入的组-在这种情况下,是一个组织,在该组织中,他们的电子邮件更改为组织电子邮件,等待组织所有者确认。
我认为这个功能(我已经从下面的设计红宝石文档中复制了该功能)将有所帮助。
#postpone_email_change_until_confirmation_and_regenerate_confirmation_token ⇒ Object (protected)
253
# File 'lib/devise/models/confirmable.rb', line 247
def postpone_email_change_until_confirmation_and_regenerate_confirmation_token
@reconfirmation_required = true
self.unconfirmed_email = self.email
self.email = self.email_was
self.confirmation_token = nil
generate_confirmation_token
end
我的问题是我不了解它的工作方式或如何将其整合到我的应用中。
我是否将其添加到我的用户模型(只需复制并粘贴),然后在用户控制器中添加一行以进行更新操作,以便对方法进行列出的每个步骤都对电子邮件进行任何更改?
如果这样做,我给控制器调用起什么名字?
在user.rb中,我尝试过:
def postpone_email_change_until_confirmation_and_regenerate_confirmation_token
@reconfirmation_required = true
self.unconfirmed_email = self.email
self.email = self.email_was
self.confirmation_token = nil
generate_confirmation_token
end
在用户控制器-更新操作中,我尝试过:
if @user.update(user_params)
user.postpone_email_change_until_confirmation_and_regenerate_confirmation_token
我显然试图错误地做到这一点。 我不清楚我应该如何使用此功能。
谁能看到我哪里出问题了?
该方法值得Confirmable
。 因此,您只需要在user.rb
定义它
例如
# Extensions
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable,
:lastseenable, :timeoutable
考虑:confirmable
符号。
然后,如果您要更改电子邮件,则before_update
和after_commit
回调将在所有条件返回真值的情况下被调用
您不能在任何地方调用受保护的实例方法。
对于受保护的方法,可以从属于同一类的任何对象的范围内调用它们。 这意味着,当将Person对象p1作为参数传递给另一个Person对象p2上的方法时,可以访问该方法中p1的所有受保护方法。
从这里
module Confirmable
extend ActiveSupport::Concern
included do
# ...
after_commit :send_on_create_confirmation_instructions, on: :create, if: :send_confirmation_notification?
after_commit :send_reconfirmation_instructions, on: :update, if: :reconfirmation_required?
#...
before_update :postpone_email_change_until_confirmation_and_regenerate_confirmation_token, if: :postpone_email_change?
end
这是调用回调的地方。
然后user.unconfirmed_email
将重新确认电子邮件发送给user.unconfirmed_email
您可能需要覆盖重新确认说明电子邮件。 这是一个例子:
# app/mailers/devise/mailer.rb
if defined?(ActionMailer)
class Devise::Mailer < Devise.parent_mailer.constantize
include Devise::Mailers::Helpers
def confirmation_instructions(record, token, opts = {})
@token = token
if record.pending_reconfirmation?
devise_mail(record, :reconfirmation_instructions, opts)
else
devise_mail(record, :confirmation_instructions, opts)
end
end
end
end
和邮件视图
# app/views/devise/reconfirmation_instructions.text.erb
Hello, <%= @resource.fullname %>!
Please reconfirm your new email: <%= your_url_with_@token" %>
--
WBR, support team
希望能有所帮助。
更新
您需要启用确认/重新确认只是将所需的策略添加到User
模型中
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable,
:lastseenable, :timeoutable
并覆盖设计邮件
# app/mailers/devise/mailer.rb
if defined?(ActionMailer)
class Devise::Mailer < Devise.parent_mailer.constantize
include Devise::Mailers::Helpers
def confirmation_instructions(record, token, opts = {})
@token = token
if record.pending_reconfirmation?
devise_mail(record, :reconfirmation_instructions, opts)
else
devise_mail(record, :confirmation_instructions, opts)
end
end
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.