【问题标题】:Fixtures in mail preview邮件预览中的装置
【发布时间】:2016-09-15 15:36:30
【问题描述】:

具有以下邮件预览器代码:

class RegistrationMailerPreview < ActionMailer::Preview

  # Preview this email at http://localhost:3000/rails/mailers/registration_mailer/welcome
  def welcome
    RegistrationMailer.welcome users(:one)
  end

end

(完整文件)。

无法访问我的灯具 (users(:one)),返回 500 错误状态并打印出以下错误:

NoMethodError: undefined method `users' for #RegistrationMailerPreview

我们可以从邮件预览器中获取灯具条目吗?

如果是,我想知道该怎么做。
我已经看到这应该是可能的here,但是我不能在这个文件中要求test_helper(我不知道为什么)而且我不明白ActionMailer::TestCaseActionMailer::Preview 之间的区别。

如果没有,有没有办法在不发送参数User.first 的情况下预览邮件,因为我可以在数据库中没有填充数据的机器上进行测试。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5


    【解决方案1】:

    我不知道默认的夹具框架,但我可以说,将 FactoryBot 用于我的夹具,我可以在我的邮件预览中使用它们,只需在构建/创建方法前加上 FactoryBot。我不需要require 文件顶部的任何内容。即:

    class RegistrationMailerPreview < ActionMailer::Preview
      def welcome
        user = FactoryBot.create(:user)
        RegistrationMailer.welcome(user)
      end
    end
    

    要回答您的第二个问题,您也可以简单地将上面的固定线替换为user = User.new(firstname: "Joe")。这将创建一个新用户以在预览中使用,而无需将其持久化到数据库中。

    【讨论】:

    • 谁能解释为什么这个解决方案被否决了?这似乎是一种将固定装置放入邮件预览的实用方法
    猜你喜欢
    • 2016-11-02
    • 2013-03-12
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 2014-10-23
    • 2017-12-08
    • 1970-01-01
    • 2020-11-08
    相关资源
    最近更新 更多