#<Contact:0x007fc2fe6a01f8> 和 gem 'mail_form' 的未定义方法“交付”
undefined method `deliver' for #<Contact:0x007fc2fe6a01f8> with gem 'mail_form'
我正在尝试下载此 gem https://github.com/heartcombo/mail_form,但我收到此错误 undefined method `deliver' for #
在 gem 文件中
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 }
在接触控制器中
class 联系人控制器 < 应用程序控制器
需要 'mail_form'
定义新
@联系人=Contact.new
结束
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
为了使用 mail_form
gem,您需要像描述中那样创建另一个模型,或者您需要修改现有模型。你不能直接使用这些行
contact_attributes = params.require(:contact).permit([:name,:email,:message,:phone])
@contact = Contact.new(contact_attributes)
@contact.deliver
在这里交付对这样的模型没有任何意义class Contact < ApplicationRecord
,直到你像这样建立你的联系模型
class Contact < ActiveRecord::Base
include MailForm::Delivery
attributes :name, :email, :message, :form
def headers
{
:subject => "My Contact Form",
:to => "nourfiverr@gmail.com",
:from => %("#{name}" <#{email}>)
}
end
end
如果您不想进行此修改,只需按照 gem 自述文件中的步骤操作即可。
我正在尝试下载此 gem https://github.com/heartcombo/mail_form,但我收到此错误 undefined method `deliver' for # 在 gem 文件中
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 }
在接触控制器中
class 联系人控制器 < 应用程序控制器 需要 'mail_form' 定义新 @联系人=Contact.new 结束
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
为了使用 mail_form
gem,您需要像描述中那样创建另一个模型,或者您需要修改现有模型。你不能直接使用这些行
contact_attributes = params.require(:contact).permit([:name,:email,:message,:phone])
@contact = Contact.new(contact_attributes)
@contact.deliver
在这里交付对这样的模型没有任何意义class Contact < ApplicationRecord
,直到你像这样建立你的联系模型
class Contact < ActiveRecord::Base
include MailForm::Delivery
attributes :name, :email, :message, :form
def headers
{
:subject => "My Contact Form",
:to => "nourfiverr@gmail.com",
:from => %("#{name}" <#{email}>)
}
end
end
如果您不想进行此修改,只需按照 gem 自述文件中的步骤操作即可。