ruby-on-rails - 如何自定义 Devise 的 "resend confirmation email"

标签 ruby-on-rails devise

我有一个自定义邮件程序 (UserMailer.rb) 和一些方法来覆盖欢迎电子邮件和忘记密码电子邮件的默认 Devise 方法。邮件程序使用自定义模板来设计电子邮件的样式,而且效果很好。

config/initializers中,我有一个文件

module Devise::Models::Confirmable
    # Override Devise's own method. This one is called only on user creation, not on subsequent address modifications.

  def send_on_create_confirmation_instructions
    UserMailer.welcome_email(self).deliver
  end

  ...
end

(同样,UserMailer 已设置完毕,非常适合发送欢迎电子邮件和重置密码电子邮件。)

但是“重新发送确认指令”选项不起作用。它使用默认的 Devise 样式发送,我希望它使用我的邮件程序布局的样式。我知道我可以手动将布局添加到默认的 Devise 布局,但我希望保持 DRY 有效,而不必这样做。

我已尝试重写 send_confirmation_instructions 方法 found here ,但我在 create(gem) devise-2.2.3/app/controllers/devise/confirmations_controller.rb 中收到 参数数量错误 (1 for 0) 错误> 在

7 # POST /resource/confirmation
8   def create
9     self.resource = resource_class.send_confirmation_instructions(resource_params)

在我的初始化程序文件中,我可以通过为 Devise 添加新的覆盖来解决此错误,但我可能没有正确执行此操作:

module Devise::Models::Confirmable::ClassMethods
  def send_confirmation_instructions
    UserMailer.send_confirmation_instructions(self).deliver
  end
end

有什么想法吗?

最佳答案

您不必通过该初始化程序来执行此操作。我通过重写确认 Controller 来完成此操作。我的设计路线如下:

devise_for :user, :path => '', :path_names => { :sign_in => 'login', :sign_out => 'logout', :sign_up => 'signup'}, 
:controllers => { 
  :sessions => "sessions", 
  :registrations => "registrations", 
  :confirmations => "confirmations"
}

然后,创建 confirmations_controller 并扩展 Devise::ConfirmationsController 以覆盖:

class ConfirmationsController < Devise::ConfirmationsController

在该 Controller 中,我有一个创建方法来覆盖默认值:

def create

  @user = User.where(:email => params[:user][:email]).first

  if @user && @user.confirmed_at.nil?
    UserMailer.confirmation_instructions(@user).deliver
    flash[:notice] = "Set a notice if you want"
    redirect_to root_url
  else
    # ... error messaging or actions here
  end

end

显然,在 UserMailer 中您可以指定用于显示确认消息的 html/文本模板。 confirmation_token 应该是 @user 模型的一部分,您可以使用它来创建具有正确 token 的 URL:

<%= link_to 'Confirm your account', confirmation_url(@user, :confirmation_token => @user.confirmation_token) %>

关于ruby-on-rails - 如何自定义 Devise 的 "resend confirmation email",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16422021/

相关文章:

ruby-on-rails - Mocha 铁路怪异

ruby-on-rails - rails 6 : Automatically reload local gem on change

ruby-on-rails - 使用 omniauth 后登录页面卡在重定向循环中。虽然它之前工作正常

mysql - 使用另一个表中的 ID 创建新表条目

ruby-on-rails - ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: 关系 "wikis"不存在

ruby-on-rails - 在 PostgreSQL 的 HSTORE 中查找键值对

ruby-on-rails - 如何对数组中的值求和?

ruby-on-rails - slug 更改时自动重写

ruby-on-rails - Rails JSON API 的简单 token 身份验证注销

ruby-on-rails - Rails Devise - 使用关联模型注册用户