So, I've checked all the existing answers to similar questions here on stackoverflow and elsewhere, but cannot get the mail_form gem to work as advertised.
Here's the setup: I'm trying to create a simple lead capture form for my company's website. I want to email the data collected by the form to my email account without a database backend and I thought that's what mail_form would make easy.
Here is my model, 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 => "[email protected]",
:from => %("#{name}" <#{email}>)
}
end
end
And here's my controller, 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
And here is my view, 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 %>
I'm pretty new to rails and web programming in general, so it might well be that I am missing something REALLY basic here that isn't mentioned in any online tutorials. I've been all over the mail_form documentation and every tutorial and answer I can find and I'm still getting the error.
Oh yes! I also get the error if I try ContactForm.new in the rails console. Any help would be greatly appreciated!
Oh yes, Rails version 3.1.1
If you're trying this for the first time from console, be sure to restart your rails console after installing your gem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With