Rails-使用布局对所有电子邮件使用相同的附件 [英] Rails - Use same attachment for all emails using layout

查看:64
本文介绍了Rails-使用布局对所有电子邮件使用相同的附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能缺少明显的东西,但是我想将徽标包含在从我的应用发送的所有电子邮件中。我有一个用于所有这些邮件程序的主版式。我认为有一种方法可以使它保持DRY状态,而不必在每一个mailer方法中添加代码行来附加文件。有人可以指出我正确的方向还是纠正我的思路。

I'm probably missing something obvious, but I've got a logo I'd like to include in all of the emails I send from my app. I have a master layout I'm using for all of those Mailers. I assume there's a way to do keep it DRY and not have to add the line of code to attach the file in every mailer method. Can someone point me in the right direction or correct my line of thought.

谢谢!

推荐答案

支持使用before_filter和after_filter进行回调在将来的Rails版本中:

Callbacks using before_filter and after_filter will be supported in a future Rails release:

http://github.com / rails / rails / commit / 4f28c4fc9a51bbab76d5dcde033c47aa6711339b

由于它们将使用AbstractController :: Callbacks实现,因此您可以执行以下操作来模仿将要使用的功能一旦Rails 4发布,ActionMailer :: Base中就会出现:

Since they will be implemented using AbstractController::Callbacks, you can do the following to mimic the functionality that will be present in ActionMailer::Base once Rails 4 is released:

class YourMailer < ActionMailer::Base
  if self.included_modules.include?(AbstractController::Callbacks)
    raise "You've already included AbstractController::Callbacks, remove this line."
  else
    include AbstractController::Callbacks
  end

  before_filter :add_inline_attachments!

  private
  def add_inline_attachments!
    attachments.inline["footer.jpg"] = File.read('/path/to/filename.jpg')
  end
end

这包括将在将来的rails版本中使用的模块,因此您可以使用的回调挂钩将相同,以确保将来的兼容性。当您尝试升级到已经包含AbstractController :: Callbacks的Rails版本时,该代码将上升,因此将提醒您删除条件逻辑。

This includes the module that will be used in a future rails version, so the callback hooks available to you will be the same to ensure future compatibility. The code will raise when you try to upgrade to a Rails version that already includes AbstractController::Callbacks, so you will be reminded to remove the conditional logic.

这篇关于Rails-使用布局对所有电子邮件使用相同的附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆