Rails ActionMailer预览不在开发中呈现

时间:2015-05-20 01:02:20

标签: ruby-on-rails actionmailer

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

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

以下是一些服务器日志:

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小时,所以任何帮助都会非常感激:)

以下是相关文件。

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

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

应用程序/视图/布局/ mailers.html.erb:

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

应用程序/视图/布局/ mailers.text.erb:

<%= yield %>

应用程序/邮寄者/ 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

应用程序/视图/ 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) %>

应用程序/视图/ 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) %>

我已经尝试在布局/邮件程序文件中添加一些额外的文本,但这也不会呈现。所以我当时认为布局没有被正确调用。但是,当我将layout 'something'放在application_mailer.rb中时,我发现缺少布局的错误。我评论了@agent = agent并在@agent.activation_token得到了一个零例外。

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

1 个答案:

答案 0 :(得分:2)

您不能多次拨打“邮件”。

应用程序/邮寄者/ 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