我正在尝试下载这个 gem https://github.com/heartcombo/mail_form我收到这个错误 undefined method `deliver' for # In the gem file
gem 'mail_form'
gem 'rails', '4.2.11.1'
在 production.rb 文件中
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: 'gmail email',
password: 'gmail password',
authentication: 'plain',
enable_starttls_auto: true }
在接触控制器
类 ContactsController < ApplicationController 需要 'mail_form' def new @contact=Contact.new end
def create
@contact = Contact.new(contact_attributes)
if @contact.deliver
redirect_to new_contact_path, notice: "Thank you... Your Message was sent successfully."
else
flash.now[:error] = "Please correct the form"
redirect_to new_contact_path
end
end
def index
@contact=Contact.all
end
private
def contact_attributes
contact_attributes = params.require(:contact).permit([:name,:email,:message,:phone])
end
end
在 new_contact_path
<%= form_for @contact do |f| %>
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Name *</label>
<%= f.text_field :name, class: "form-control" ,required: "required" %>
</div>
<div class="form-group">
<label>Email *</label>
<%= f.text_field :email, class: "form-control", required: "required" %>
</div>
<div class="form-group">
<label>Phone</label>
<%= f.text_field :phone, class: "form-control" %>
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Message *</label>
<%= f.text_area :message , id: "message", required: "required", class: "form-control" ,rows: "8" %></textarea>
</div>
<div class="form-group">
<%= f.submit "Submit Message" %></button>
</div>
</div>
<% end %>
在接触模型文件中
def headers
{
:subject => "My Contact Form",
:to => "nourfiverr@gmail.com",
:from => %("#{name}" <#{email}>)
}
end