0

I've been following this blog post regards creating a 'contact me' form and mailer. I've got far enough that my form displays Messaged Received but I never receive an email, not in Span folder either. I have sendgrid working fine with Devise for user authentication so I know that aspect is working.

Messages/new.html.erb

<%= form_for @message, url: create_message_url do |f| %>
  <%= notice %>
  <%= @message.errors.full_messages.join(', ') %>

  <%= f.text_field :name, placeholder: 'name' %>
  <%= f.email_field :email, placeholder: 'email' %>
  <%= f.text_area :body, placeholder: 'body' %>
  <%= f.submit 'Send' %>
<% end %>

controllers/messages_controller.rb

class MessagesController < ApplicationController

def new
  @message = Message.new
end

def create
  @message = Message.new message_params

  if @message.valid?
    redirect_to new_message_url, notice: "Message received, thanks!"
  else
    render :new
  end
end

private

def message_params
  params.require(:message).permit(:name, :email, :body)
end

end

mailers/message_mailer.rb

class MessageMailer < ApplicationMailer
  def contact_me(message)
    @body = message.body

    mail to: "<mydomainemail>", from: message.email
  end
end

Testing throws no errors, and I can also preview the email ok.

Anything else I'm missing?

4

1 回答 1

1

在我看来,您实际上并没有触发控制器中的发送事件。如果我正确理解您的流程,您的 create 方法应该在邮件和消息上具有 .deliver 方法。

def create
  @message = Message.new message_params

  if @message.valid?
    MessageMailer.contact_me(@message).deliver <-- This
    redirect_to new_message_url, notice: "Message received, thanks!"
  else
    render :new
  end
end
于 2017-04-26T14:22:31.880 回答