Created
July 4, 2013 03:46
-
-
Save anonymous/5924775 to your computer and use it in GitHub Desktop.
The code to send a basic contact form in Rails 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ContactUsMailer < ActionMailer::Base | |
default from: "website@auxoapp.com" | |
default to: "tj@auxoapp.com" | |
def new_message(message) | |
@message = message | |
mail(:subject => "Contact Submission from Auxoapp") | |
end | |
end | |
class Message | |
include ActiveModel::Validations | |
include ActiveModel::Conversion | |
extend ActiveModel::Naming | |
attr_accessor :name, :email, :body | |
validates :name, :email, :body, :presence => true | |
validates :email, :format => { :with => %r{.+@.+\..+} } | |
def initialize(attributes = {}) | |
attributes.each do |name, value| | |
send("#{name}=", value) | |
end | |
end | |
def persisted? | |
false | |
end | |
end | |
#Controller | |
def home | |
@message = Message.new | |
@tweets = Twitter.user_timeline('auxoapp', {:count => "3"}) | |
end | |
def terms | |
#This method handles the Terms and Conditions page | |
end | |
def create_message | |
#This method handles Homepage contact form delivery | |
@message = Message.new(params[:message]) | |
respond_to do |format| | |
if @message.valid? | |
format.js | |
ContactUsMailer.new_message(@message).deliver | |
else | |
format.js | |
end | |
end | |
end | |
#contact_us_mailer.html.erb | |
Name: <%= @message.name %> | |
Email: <%= @message.email %> | |
Body: <%= @message.body %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment