Rails ActionMailer 预览未在开发中呈现

Rails ActionMailer preview not rendering in development

我已经设置了帐户激活电子邮件。当我进入预览页面时: http://localhost:3000/rails/mailers/agent_mailer/account_activation

视图是空白的。该页面正确地格式化了 to/from 地址和主题,但电子邮件没有正文。

以下是一些服务器日志:

Started GET "/rails/mailers/agent_mailer/account_activation?part=text%2Fhtml" for ::1 at 2015-05-19 17:42:27 -0700
Processing by Rails::MailersController#preview as HTML
  Parameters: {"part"=>"text/html", "path"=>"agent_mailer/account_activation"}
  Agent Load (0.5ms)  SELECT  "agents".* FROM "agents"  ORDER BY "agents"."id" ASC LIMIT 1
  Rendered agent_mailer/account_activation.html.erb within layouts/mailer (0.1ms)
  Rendered agent_mailer/account_activation.text.erb within layouts/mailer (0.0ms)
  Rendered agent_mailer/account_activation.html.erb within layouts/mailer (0.1ms)
  Rendered agent_mailer/account_activation.html.erb within layouts/mailer (0.0ms)

AgentMailer#account_activation: processed outbound mail in 43.5ms
  Rendered text template (0.0ms)
Completed 200 OK in 49ms (Views: 1.8ms | ActiveRecord: 0.5ms) 

我不明白为什么要渲染 account_activation.html.erb 3 次。 我已经用头撞墙 6 个小时了,所以非常感谢任何帮助:)

这是相关文件。

app/mailers/application_mailer.rb:

class ApplicationMailer < ActionMailer::Base
  default from: "admin@example.com"
  layout 'mailer'
end

app/views/layouts/mailers.html.erb:

<html>
  <body>
   <%= yield %>
  </body>
</html>

app/views/layouts/mailers.text.erb:

<%= yield %>

app/mailers/agent_mailer.rb:

class AgentMailer < ApplicationMailer
  def account_activation(agent)
    @agent = agent
    mail from: "admin@example.com"
    mail to: agent.email
    mail subject: "Agent Account Activation"
  end
end

app/views/agent_mailer/account_activation.html.erb:

<p>Welcome to Example! Click on the link below to activate your agent account: </p>

<%= link_to "Activate", edit_agent_account_activation_url(@agent.activation_token, email: @agent.email) %>

app/views/agent_mailer/account_activation.text.erb:

Welcome to Example! Click on the link below to activate your agent account:

<%= edit_agent_account_activation_url(@agent.activation_token, email: @agent.email) %>

我试过在 layouts/mailers 文件中添加一些额外的文本,但也没有呈现。所以我当时认为布局没有被正确调用。但是,当我将 layout 'something' 放入 application_mailer.rb 时,我收到了缺少布局的错误消息。我注释掉了 @agent = agent 并在 @agent.activation_token.

处得到了一个零异常

所以看起来布局和模板正在处理中,但由于某种原因它们没有被渲染。

你不能多次调用 'mail'。

app/mailers/agent_mailers.rb:

   class AgentMailer < ApplicationMailer
     def account_activation(agent)
       @agent = agent
       mail from: "admin@example.com", to: agent.email, subject: "Agent Account Activation"
     end
   end