我已经用 mail_form gem 建立了一个联系表。我有两个不同的联系人需要在两个不同的邮件上接收消息。所以我已经完成了联系人和消息控制器以及联系人和消息模型。我可以使用联系人表单发送电子邮件,但不能使用消息表单发送电子邮件。
当我尝试使用消息表单发送电子邮件时,我只会从服务器收到此消息:
Started POST "/messages" for 127.0.0.1 at 2020-07-20 10:11:27 +0200
Processing by MessagesController#create as HTML
Parameters: {"authenticity_token"=>"lQKACWLv72xM6tjq9wvpWNbKZ65QMpdXpnrrGIIemvfFmPfAEOoDtQN/
qUSjU7lZdhTHcqKEPMblhf5zHUtgRg==", "message"=>{"name"=>"me", "email"=>"email.email@gmail.com",
"message"=>"Hello", "nickname"=>""}, "commit"=>"Send Message"}
Rendering messages/new.html.erb within layouts/application
Rendered messages/new.html.erb within layouts/application (Duration: 2.7ms | Allocations: 959)
[Webpacker] Everything's up-to-date. Nothing to do
而且我不知道如何检查服务器显示的其他错误消息....
所以这发送电子邮件:
应用程序/控制器/contacts_controller.rb:
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(params[:contact])
@contact.request = request
if @contact.deliver
flash.now[:error] = nil
redirect_to root_path, notice: 'Message sent successfully'
else
flash.now[:error] = 'Cannot send message'
render :new
end
end
end
应用程序/模型/contact.rb
class Contact < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message, :validate => true
attribute :nickname, :captcha => true
def headers
{
:subject => "Contact Form",
:to => "email.email@gmail.com",
:from => %("#{name}" <#{email}>)
}
end
end
应用程序/视图/联系人/new.html.erb
<%= form_for @contact do |f| %>
<% if flash[:error].present? %>
<p> flash[:error]</p>
<% end %>
<%= f.label :name %> <br>
<%= f.text_field :name, required: true %>
<br>
<%= f.label :email %> <br>
<%= f.text_field :email, required: true %>
<br>
<%= f.label :message %> <br>
<%= f.text_area :message, as: :text %>
<%= f.label :nickname %> <br>
<%= f.text_field :nickname, hint: 'leave this field blank' %>
<%= f.submit 'Send Message', class: "button" %>
<% end %>
还有一个不发送电子邮件的:
应用程序/控制器/messages_controller.rb:
class MessagesController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(params[:contact])
@message.request = request
if @message.deliver
flash.now[:error] = nil
redirect_to root_path, notice: 'Message sent successfully'
else
flash.now[:error] = 'Cannot send message'
render :new
end
end
end
应用程序/模型/message.rb:
class Message < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message, :validate => true
attribute :nickname, :captcha => true
def headers
{
:subject => "Contact Form",
:to => "email@gmail.com",
:from => %("#{name}" <#{email}>)
}
end
end
应用程序/视图/消息/new.html.erb:
<%= form_for @message do |f| %>
<% if flash[:error].present? %>
<p> flash[:error]</p>
<% end %>
<%= f.label :name %> <br>
<%= f.text_field :name, required: true %>
<br>
<%= f.label :email %> <br>
<%= f.text_field :email, required: true %>
<br>
<%= f.label :message %> <br>
<%= f.text_area :message, as: :text %>
<%= f.label :nickname %> <br>
<%= f.text_field :nickname, hint: 'leave this field blank' %>
<%= f.submit 'Send Message', class: "button" %>
<% end %>