mail_form gem for Contact Form

704 views Asked by At

I want to create a Contact form for which I found mail_form gem in some forum. I tried to using it but didn't get it working. Following is my code snippet: Ruby 1.8.7 Rails 2.3.15 mail_form 1.0.0

contact_form_controller.rb

require 'mail_form'

class ContactFormController < ApplicationController
  def new
    @contact_form = ContactForm.new
    respond_to do |format|
      format.html
      format.xml
    end
  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 message!'
      else
        render :new
      end
    end
  end

end

contact_form.rb require 'mail_form'

class ContactForm < MailForm
  subject "My Contact Form"
  recipients "[email protected]"
  sender{|c| %{"#{c.name}" <#{c.email}>}}


end

new.html.erb

<% form_for @contact_form do |f| %>
<%= f.error_messages %>
  <%= f.label :name %>
  <%= f.text_field :name %><br/>

  <%= f.label :email %>
  <%= f.text_field :email %><br/>

  <%= f.lable :message %>
  <%= f.text_area :message %><br/>

  <%= f.submit 'Submit' %>
<% end %>

Any suggestions would help me lot. Thanks in advance.

1

There are 1 answers

0
Oswaldo Ferreira On

According to docs, your ContactForm should be like this:

class ContactForm < MailForm
  subject "My Contact Form"
  recipients "[email protected]"
  sender{|c| %{"#{c.name}" <#{c.email}>} }

  attribute :name,      :validate => true
  attribute :email,     :validate => /[^@]+@[^\.]+\.[\w\.\-]+/
  attribute :message
end

From: http://rubydoc.info/gems/mail_form/1.0.0/frames