Skip to content

Instantly share code, notes, and snippets.

@seanrucker
Created May 14, 2012 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanrucker/2694480 to your computer and use it in GitHub Desktop.
Save seanrucker/2694480 to your computer and use it in GitHub Desktop.
<!-- app/views/inquiries/_form.html.erb -->
<%= form_for inquiry do |f| %>
<div>
<%= f.label :name %><br/>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div>
<%= f.label :message %><br/>
<%= f.text_area :message %>
</div>
<div>
<%= f.submit "Send" %>
</div>
<% end %>
<!-- app/views/inquiries/_form.html.erb -->
<%= form_for inquiry do |f| %>
<% if inquiry.errors.any? %>
<ul>
<% inquiry.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<div>
<%= f.label :name %><br/>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div>
<%= f.label :message %><br/>
<%= f.text_area :message %>
</div>
<div>
<%= f.submit "Send" %>
</div>
<% end %>
# app/controllers/inquiries_controller.rb
class InquiriesController < ApplicationController
def new
@inquiry = Inquiry.new
end
def create
@inquiry = Inquiry.new(params[:inquiry])
if @inquiry.deliver
render :thank_you
else
render :new
end
end
end
# app/models/inquiry.rb
class Inquiry
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_accessor :name, :email, :message
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def deliver
true
end
def persisted?
false
end
end
# app/models/inquiry.rb
class Inquiry
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
attr_accessor :name, :email, :message
validates :name,
:presence => true
validates :email,
:format => { :with => /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/ }
validates :message,
:length => { :minimum => 10, :maximum => 1000 }
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def deliver
return false unless valid?
true
end
def persisted?
false
end
end
# app/models/inquiry.rb
class Inquiry
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
include ActionView::Helpers::TextHelper
attr_accessor :name, :email, :message
validates :name,
:presence => true
validates :email,
:format => { :with => /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/ }
validates :message,
:length => { :minimum => 10, :maximum => 1000 }
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def deliver
return false unless valid?
Pony.mail({
:from => %("#{name}" <#{email}>),
:reply_to => email,
:subject => "Website inquiry",
:body => message,
:html_body => simple_format(message)
})
end
def persisted?
false
end
end
<!-- app/views/inquiries/new.html.erb -->
<h1>Want to get in touch?</h1>
<p>Please fill out the form below and we'll get back to you as soon as possible.</p>
<%= render 'form', :inquiry => @inquiry %>
# config/initializers/pony.rb
Pony.options = {
:to => 'you@example.com',
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'user',
:password => 'password',
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
}
}
# config/routes.rb
resources :inquiries, :only => [:new, :create] do
get 'thank_you', :on => :collection
end
<!-- app/views/inquiries/thank_you.html.erb -->
<h1>Thank you!</h1>
<p>Your inquiry has been sent and we'll be in touch as soon as possible.</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment