我有一个 Rails 应用程序,我需要一个联系表。我正在使用 mail_form gem,我可以从 rails 控制台发送邮件,但不能从表单发送邮件。遵循了几个视频和教程,但似乎没有一个可以解决这个问题。
控制器:
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contacts_params)
@contact.request = request
if @contact.deliver
flash.now[:notice] = 'Thank you for your message. We will contact you soon!'
else
flash.now[:alert] = 'Cannot send message'
render :new
end
end
protected
def contacts_params
params.require(:contact).permit(:name, :email, :message)
end
end
联系人.rb
class Contact < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :file, :attachment => true
attribute :message
attribute :nickname, :captcha => true
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "My Contact Form",
:to => "tcunha_lp@hotmail.com",
:from => %("#{name}" <#{email}>)
}
end
end
服务器日志显示:
Started POST "/contacts" for 127.0.0.1 at 2019-02-19 11:40:45 +0000
Processing by ContactsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"yLolqR/aOQ1T75HJr4f+DlGwbw6e67FwVoLuWNr/MIs2PdFbdx/626SUqTjw0xbb+9xmy4zg2mzZKYt9XXYNdA==", "contact"=>{"name"=>"", "email"=>"", "message"=>""}, "commit"=>"Send"}
Rendering contacts/new.html.erb within layouts/application
Rendered contacts/new.html.erb within layouts/application (4.6ms)
Rendered layouts/_navbar.html.erb (0.7ms)
Rendered layouts/_header.html.erb (53.4ms)
Rendered layouts/_navbar.html.erb (0.2ms)
Rendered layouts/_footer.html.erb (51.8ms)
Completed 200 OK in 767ms (Views: 761.6ms | ActiveRecord: 0.0ms)
环境配置应该没问题,因为我可以从控制台发送邮件..
有任何想法吗?