Ruby on Rail - 部分与邮件本地

时间:2013-12-06 09:43:01

标签: ruby-on-rails ruby ruby-on-rails-3.2 actionmailer partial

我正在尝试发送这样的电子邮件

mail(:to => @user.email, :from => from_email, :subject => t('orders.invoice.email_subject'), :content_type => "text/html") do |format|
    format.html { render partial: "notifier/follow_up_email.#{I18n.locale}.html.erb", locals: {storefront: storefront, order: order} }
end

不幸的是,rails只是在寻找follow_up_email.en.html而不是follow_up_email.en.html.erb - 我做错了什么?

1 个答案:

答案 0 :(得分:0)

render partial: ...不应包含en.html.erb等文件扩展名,因为它们是自动添加的。

http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html

但为什么你使用局部邮件,你不能创建一个合适的视图吗?

<强>更新

这些都在http://guides.rubyonrails.org/v3.2.16/action_mailer_basics.html

指南中有所描述

应用程序/邮寄者/ user_mailer.rb:

class UserMailer < ActionMailer::Base

  default from: "no-reply@memyselfandi.com"

  def notification(user, storefront, order)
    @user, @storefront, @order = user, storefront, order
    I18n.locale = @user.locale    # see note below
    mail to: @user.email, subject: I18n.t('orders.invoice.email_subject')
  end

end

应用程序/视图/ user_mailer文件/ notification.en.html.erb:

<html><body>
This is a mail for <%= @user.email %> yadda yadda. Use @storefront and @order here.
</body></html>

应用程序/视图/ user_mailer文件/ notification.de.html.erb:

<html><body>
Das ist ein mail für <%= @user.email %> bla bla.
</body></html>

只有在您要发送异步邮件时才需要I18n.locale = @user.locale行。从cronjob触发rake任务时。