pthamm
pthamm

Reputation: 1841

Rails ActionMailer preview not rendering in development

I've set up account activation emails. When I go to the preview page: http://localhost:3000/rails/mailers/agent_mailer/account_activation

The view is blank. The page correctly formats the to/from addresses and the subject, but there is no body to the email.

Here are some of the server logs:

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) 

I don't understand why it's rendering account_activation.html.erb 3 times. I've been banging my head against a wall for 6 hours so any help would be really appreciated :)

Here are the relevant files.

app/mailers/application_mailer.rb:

class ApplicationMailer < ActionMailer::Base
  default from: "[email protected]"
  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: "[email protected]"
    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) %>

I've tried putting some extra text in the layouts/mailers file but that doesn't render either. So I thought then that the layouts weren't being properly called. However, when I put layout 'something' in the application_mailer.rb I got an error for a missing layout. I commented out @agent = agent and got an nil exception at the @agent.activation_token.

So it seems like the layout and the template are being processed, but for some reason they aren't being rendered.

Upvotes: 1

Views: 1240

Answers (1)

pthamm
pthamm

Reputation: 1841

You can't call 'mail' so many times.

app/mailers/agent_mailers.rb:

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

Upvotes: 2

Related Questions