使用 rails 中的 Mail_form Gem,我可以验证我的邮件配置是否正确;但是我的日志中没有收到任何错误。
联系人.rb [app/models/contact.rb]
class Contact < MailForm::Base
include MailForm::Delivery
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message
attribute :nickname, :captcha => false
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "IT Site Contact",
:to => "kompinski@gmail.com",
:from => %("#{name}" <#{email}>)
}
end
end
联系人控制器.rb
class ContactsController < ApplicationController
def new
@contact = Contact.new(params[:contact])
end
def create
@contact = Contact.new(params[:contact])
@contact.request = request
@contact.deliver
end
end
路线.rb
resources :contacts, only: [:new, :create]
get 'contact' => 'contacts#new'
post 'contact' => 'contacts#create'
new.html.erb [/views/contacts/new.html.erb]
<div class="container">
<div class="col-md-6"><h2>Contact us</h2>
<hr />
<form role="form">
<%= twitter_bootstrap_form_for @contact, url: new_contact_path, method: :post do |f| %>
<div class="medium">
</div>
<div class="medium">
<%= f.text_field :name, :required => true %>
</div>
<div class="medium">
<%= f.email_field :email, :required => false %>
</div>
<div class="medium">
<%= f.text_area :message, :as => :text, :required => true %>
</div>
<div class="btn">
<%= f.submit 'Submit' %>
<% end %>
</div>
</form>
</div>
</div>
** 编辑 ** 我也可以使用 Contact.new(params[:contact]).deliver 直接从表单发送,但仅在重新提交地址时;但是,当我加载包含表单的页面时,它指示 TrueClass:Class 的未定义方法 `model_name'