Created
February 10, 2012 17:01
-
-
Save mklickman/1790914 to your computer and use it in GitHub Desktop.
Basic Rails Contact Form and Mailer
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
<%= form_for(@contact) do |f| %> | |
<% if @contact.errors.any? %> | |
<div id="error_explanation"> | |
<h3><%= pluralize(@contact.errors.count, "error") %> prohibited this contact from being saved:</h2> | |
<ul> | |
<% @contact.errors.full_messages.each do |msg| %> | |
<li><%= msg %></li> | |
<% end %> | |
</ul> | |
</div> | |
<% end %> | |
<div class="field"> | |
<%= f.label :name %><br /> | |
<%= f.text_field :name %> | |
</div> | |
<div class="field"> | |
<%= f.label :email %><br /> | |
<%= f.text_field :email %> | |
</div> | |
<div class="field"> | |
<%= f.label :message %><br /> | |
<%= f.text_area :message %> | |
</div> | |
<div class="actions"> | |
<%= f.submit "Send"%> | |
</div> | |
<% end %> |
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 Contact < ActiveRecord::Base | |
validates :email, | |
:presence => :true, | |
:format => { | |
:with => /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i, | |
:message => "must be a valid email address" | |
} | |
validates :message, :presence => :true | |
end |
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 ContactsController < ApplicationController | |
def new | |
@contact = Contact.new | |
end | |
def create | |
@contact = Contact.new(params[:contact]) | |
if @contact.save | |
ContactsMailer.general_message(@contact).deliver | |
render :thanks | |
else | |
render :new | |
end | |
end | |
def thanks | |
end | |
end |
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
# This file lives in the app/mailers directory | |
# See http://guides.rubyonrails.org/action_mailer_basics.html for details | |
class ContactsMailer < ActionMailer::Base | |
default from: "[email protected]" | |
def general_message(contact) | |
@contact = contact | |
mail( :to => "[email protected]", :subject => "You Have a Message From Your Website") | |
end | |
end |
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
<h2>Send Us A Message</h1> | |
<%= render 'form' %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, I see what you mean. It did work!