2

根据this guide on using custom mailers,我正在尝试覆盖设计的邮件。

# config/initializers/devise.rb

config.mailer = "AccountsMailer"

真的,我想为所有邮件操作动态设置主机。但目前,似乎只是对该reset_password_instructions方法的基本覆盖甚至不起作用。

# app/mailers/accounts_mailer.rb

class AccountsMailer < Devise::Mailer
  # allow use of before_filter
  include AbstractController::Callbacks

  def default_url_options
    { host: 'overridden.com' }
  end

  def reset_password_instructions(record, opts={})
    raise 'overridden'
    @host = 'overridden.co'
    super
  end

end

这让我怀疑设计是否甚至使用我的自定义邮件。

# views/customers/mailer/reset_password_instructions.html.erb

<% puts "*Mail View*" %>
<% puts @host  %>
<% puts Devise.mailer %>
...

发送此消息时,从我的 forman 日志中:

mailer.1  | *Mail View*
mailer.1  |
mailer.1  | AccountsMailer
mailer.1  | Render customers/mailer/reset_password_instructions.html.erb

因此,Devise 声称正在使用 AccountsMailer,但方法覆盖不起作用,因为发送消息而不是引发异常,...


相关设置

  • 导轨 3.2.13
  • 红宝石 1.9.3-p374
  • 设计 2.1.2
  • 设计异步 0.5.1
  • Sidekiq 2.12.0
  • 工头 0.61.0
    • 网络:捆绑执行独角兽 -p $PORT -c ./config/unicorn.rb
    • 数据库:postgres -D /usr/local/var/postgres
    • redis: redis-server /usr/local/etc/redis.conf
    • 邮件捕手:邮件捕手 -f
    • worker: bundle exec sidekiq -q high,2 -q default -e development -c 1
    • 邮件程序: bundle exec sidekiq -q mailer -e development -c 1

免责声明

  • 是的,我重新启动了工头/我的服务器
  • 我不能静态设置action_mailer.default_url_options[:host]in environments/<env>.rb,因为主机名是根据用户类型动态设置的(员工和客户有不同的子域)
    • action_mailer.default_url_options还不接受 proc 或 block
  • 在每个视图中设置主机名太湿了,我睡不好觉

SO 搜索似乎没有看到其他人有这个问题,也没有设计 GH 问题——所以我忽略了什么?谢谢!

4

1 回答 1

2

呃,这最终是一个版本问题。

我在devisedevise-async页面上阅读的文档引用了当前行为。我分别使用 2.1.x 和 0.5.x。

devise-async在那些版本中,必须使用 using明确地覆盖邮件程序config.mailer(我没有这样做)。从 2.2.x 和 0.7.x 开始,隐式devise-async使用。Devise.mailer

我已经升级到 2.2.x 和 0.7.x,这解决了上面代码的问题。

注意:如果您从 <= 2.1.x 升级到 2.2.x,请注意 2.2 中引入的向后不兼容的更改

于 2013-06-03T20:55:12.563 回答