如何在发送延迟电子邮件时将语言环境传递给ActiveJob / SuckerPunch?

时间:2018-04-20 10:57:05

标签: ruby-on-rails rails-activejob sucker-punch

在我的Rails 5应用中,我尝试使用ActiveJobSucker Punch发送电子邮件:

# app/controllers/users_controller.rb

class UsersController < ApplicationController

  def activate
    user = User.find_by(:activation_token => params[:id])
    user.activate
    SendFollowUpEmailJob.perform_in(30, user)
    sign_in user
  end

end
# app/jobs/send_follow_up_email.rb

class SendFollowUpEmailJob < ActiveJob::Base
  include SuckerPunch::Job

  queue_as :default

  def perform(user)
    SupportMailer.follow_up(user).deliver_later
  end
end
# app/mailers/support_mailer.rb

class SupportMailer < ApplicationMailer

  layout nil

  default :from => "admin@app.com"

  def follow_up(user)
    @user = user
    mail :to => @user.email, :subject => t('.subject')
  end

end
# app/views/user_mailer/activation.html.erb

<%= link_to(t('views.activate'), activate_user_url(@user.activation_token, :locale => "#{I18n.locale}")) %>

除了电子邮件总是以默认语言环境发送而不是传递给activate操作的语言环境外,一切正常。

整个应用程序中的所有电子邮件始终以正确的语言环境发送,因此必须下载到 ActiveJob SuckerPunch

如果您能提供帮助,请告诉我。感谢。

1 个答案:

答案 0 :(得分:1)

我熟悉应用程序中的I18n,但在邮件程序中并不多。请记住邮件是在请求之外发送的(ActiveJob),所以它不知道那里发生的任何事情。看起来你没有做任何事情告诉邮件者它应该在特定的语言环境中发送....

好吧也许这有你的答案(和我!)https://niallburkley.com/blog/localize-rails-emails/

I18n.with_locale(@user.locale) do
  mail(
    to: @user.email,
    subject: I18n.t('user_mailer.new_follower.subject')
  )
end