Nick
Nick

Reputation: 3090

Action Mailer Preview: Email 'new' not found in MessageMailerPreview

I have three mailers, all of which work. But for one (the contact form) the mailer preview doesn't work and I don't understand why.

The error message:

undefined method 'email'
  def new_message(message)
    @message = message
    mail from: message.email, subject: "Message from #{message.name}"
  end

In app/mailers/message_mailer.rb I have:

class MessageMailer < ApplicationMailer
  default to: "<[email protected]>",
  return_path: "<[email protected]>"
  def new_message(message)
    @message = message
    mail from: message.email, subject: "Message from #{message.name}"
  end
end

Part of app/views/messages/new.html.erb:

<%= form_for :message, url: contact_path do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name, placeholder: 'Name', class: 'form-control' %>
  <%= f.label :email %>
  <%= f.email_field :email, placeholder: 'Email', class: 'form-control' %>
  <%= f.label :content %>
  <%= f.text_area :content, placeholder: 'Your message…', class: 'form-control' %>
  <%= f.submit "Send", class: "btn btn-primary" %>
<% end %>

test/mailers/previews/test_mailer_preview.rb:

class MessageMailerPreview < ActionMailer::Preview
    message = [name: "My Name", email: "[email protected]", content: "This is my message"]
    MessageMailer.new_message(message)
  end
end

The contact form is properly working on the development server, and the other two mailers (password reset and authentication) also work and for these last two mailers the previews also work properly. These two mailers also have model.email in the mailer, and there it does work. I don't understand why the preview for the contact form mailer produces the error message.

Upvotes: 0

Views: 1921

Answers (2)

Nick
Nick

Reputation: 3090

I turns out that if I replaced message = [name: 'My Name', email: '[email protected]', content: 'This is my message']

with

message = Message.first
message.email = "[email protected]"

or even just

message = Message.first

the preview does work. Don't understand why since I would expect the original formulation also to work.

Upvotes: 1

creativereason
creativereason

Reputation: 1524

Your mailer class isn't inherited from ActionMailer, which is fine if you have an ApplicationMailer class doing that already, can you post the code for it please? Otherwise try this.

class MessageMailer < ActionMailer::Base

Upvotes: 0

Related Questions