发送延迟电子邮件时如何将语言环境传递给 ActiveJob / SuckerPunch?
How to pass locale to ActiveJob / SuckerPunch when sending delayed email?
在我的 Rails 5 应用程序中,我正在尝试使用 ActiveJob and Sucker 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。
如果你能帮忙,请告诉我。谢谢。
我熟悉应用程序中的 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
在我的 Rails 5 应用程序中,我正在尝试使用 ActiveJob and Sucker 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。
如果你能帮忙,请告诉我。谢谢。
我熟悉应用程序中的 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