所以,我已经在stackoverflow和其他地方检查了类似问题的所有现有答案,但是无法让mail_form gem像宣传的那样工作。
这是设置:我正在尝试为我公司的网站创建一个简单的潜在客户捕获表单。我希望将表单收集的数据通过电子邮件发送到我的电子邮件帐户,而不需要数据库后端,我认为这就是mail_form的简单方法。
这是我的模型ContactForm.rb
:
class ContactForm < Mailform::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w]{2,})\z/i
attribute :file, :attachment => true
attribute :phone
attribute :referral
attribute :message
attribute :nickname, :captcha => true
def persisted?
false
end
def headers
{
:subject => "New Lead",
:to => "jonthewineguy@gmail.com",
:from => %("#{name}" <#{email}>)
}
end
end
这是我的控制器,contact_forms_controller.rb
:
class ContactFormsController < ApplicationController
def new
@contact_form = ContactForm.new
end
def create
begin
@contact_form = ContactForm.new(params[:contact_form])
@contact_form.request = request
if @contact_form.deliver
flash.now[:notice] = 'Thank you for your interest!'
redirect_to root_path
else
render :new
end
rescue ScriptError
flash[:error] = 'Sorry, something was wrong'
end
end
end
以下是我的观点,contact_forms/new.html.erb
:
<%= form_for @contact_form do |f| %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name, :required => true %>(required)<br /><br><br>
</div>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email, :required => true %>(required)
<br /><br><br>
</div>
<div class="field">
<%= f.label :phone %>
<%= f.phone_field :phone %><br /><br><br>
</div>
<div class="field">
How did you hear about us?:<br /> <%= f.text_field :referral
%><br /><br><br>
</div>
<div class="field">
Comments (What types of wine are you
interested in?):<br / <%= f.text_area :message %><br /><br><br>
</div>
<div class="field">
Submit: <%= f.submit "Create" %>
</div>
<% end %>
我对rails和Web编程一般都很陌生,所以很可能我错过了一些真正基本的东西,这在任何在线教程中都没有提到过。我已经遍历了mail_form文档以及我能找到的每个教程和答案,我仍然会收到错误。
哦,是的!如果我在rails控制台中尝试ContactForm.new,我也会收到错误。任何帮助将不胜感激!
哦,是的,Rails版本3.1.1
答案 0 :(得分:4)
你的课程定义中有拼写错误
class ContactForm < Mailform::Base
应该是
class ContactForm < MailForm::Base
还要确保已安装mail_form
gem "mail_form", ">= 1.3.0"
到您的Gemfile并运行bundle install
答案 1 :(得分:3)
如果您是第一次从控制台尝试此操作,请务必在安装gem后重新启动rails控制台。