Skip to content

Instantly share code, notes, and snippets.

@deepakdargade
Created July 24, 2013 06:26
Show Gist options
  • Save deepakdargade/6068436 to your computer and use it in GitHub Desktop.
Save deepakdargade/6068436 to your computer and use it in GitHub Desktop.
mail_form
class ContactForm < MailForm::Base
attribute :name, :validate => true
attribute :nickname, :captcha => true
attribute :email, :validate => /[^@]+@[^\.]+\.[\w\.\-]+/
attribute :message, :validate => true
def headers
origin = "#{name} <#{email}>"
return :subject => ::I18n.t(:subject, :scope => [:actionmailer, :contact_form]),
:from => origin,
:reply_to => origin,
:to => Settings.mailer.contact_email
end
def persisted?
false
end
end
class ContactsController < ApplicationController
def new
@contact = ContactForm.new(params[:contact_form])
end
def create
@contact = ContactForm.new(params[:contact_form])
if @contact.valid?
@contact.deliver
redirect_to :root, :notice => tf('contact.thanks')
else
render :action => "new"
end
end
end
gem 'mail_form', '>= 1.2.1'
= semantic_form_for @contact, :url => contact_us_path do |f|
= f.inputs :name => "Your Contact Details" do
= f.input :name
= f.input :nickname
= f.input :email
= f.inputs :name => "Your Message" do
= f.input :message, :input_html => {:rows => 8}, :as => :text
= f.buttons do
= f.commit_button "Contact Us"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment