Ruby on rails 在Rails中使用ActionMailer从表单接收电子邮件

Ruby on rails 在Rails中使用ActionMailer从表单接收电子邮件,ruby-on-rails,actionmailer,Ruby On Rails,Actionmailer,我正在尝试使用Rails 5中的ActionMailer从联系人表单接收电子邮件。但我不确定如何将表单上创建的资源的参数发送到receive方法上的电子邮件,并将其发送到应用程序的电子邮件 控制器/联系人\u controller.rb: class ContactsController < ApplicationController def index @contact = Contact.new end def create @contact = Con

我正在尝试使用Rails 5中的ActionMailer从联系人表单接收电子邮件。但我不确定如何将表单上创建的资源的参数发送到receive方法上的电子邮件,并将其发送到应用程序的电子邮件

控制器/联系人\u controller.rb:

class ContactsController < ApplicationController

  def index
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(contact_params)
    if @contact.save
      DefaultMailer.receive(@contact).deliver
      redirect_to root_path
    else
      render :new
    end
  end

  private
  def contact_params
    params.require(:contact).permit(:name, :email, :subject, :body)
  end
end
mailers/default_mailer.rb:

class DefaultMailer < ApplicationMailer
  default from: "atendimento@lcasystems.com.br"

  def receive(email)
    mail.create(subject: email.subject, body: email.body)
  end
end

它给出一个500服务器错误,并在receiver方法中为nil:NilClass显示未定义的方法'humanize'。

这是一个联系表单,因此您希望电子邮件发送给您自己,因此您需要to:key-value对

包括联系人的姓名和电子邮件也很有用,这样您就可以知道在哪里发送任何回复

def receive(contact)
  mail(to: "atendimento@lcasystems.com.br", 
       subject: contact.subject, 
       body: contact.body.to_s + 
            "\n (Sender is #{contact.name} and their email is #{contact.email})")
end