Rails model validation without database using active_form

Usually, when you create a model, you create a database, and then the model works with ActiveRecord. And it’s ActiveRecord (as far as I currently know) that lets you use validation techniques.

So how do we add validation techniques to something like a contact form on our Rails site that does not have a database?

Install the plugin active form

script/plugin install git://github.com/xgamerx/active_form.git

Then create a file in your app/models folder called email.rb. Paste in the following into that file. Line #1 is key

1
2
3
4
5
6
class Email < ActiveForm
  attr_accessor :name, :email_address, :message
  validates_presence_of :message
  validates_presence_of :email_address
  validates_presence_of :name 
end

Then create another file in app/models called contact_emailer.rb and paste in the following. This is what will let you send the email

(Wait: make sure you know how to setup email so it works. See my post on creating a contact form with gmail smtp and rails. In that post I called the following file Emailer.rb. You could do that as well, but Line #1 with ActionMailer::Base is key.)

1
2
3
4
5
6
7
8
9
10
11
class ContactMailer < ActionMailer::Base
 
  def contact_email(email)
      recipients  "site@email.com" 
      headers     "Reply-to" => "#{email.email_address}"
      from        email.name + " <" + email.email_address + ">"
      subject     "SiteName"
      body        :email => email
  end
 
end

Then in your views create a folder called contact_mailer & inside of that folder create a file called contact_email.html.erb
app
-views
–contact_mailer
—contact_email.html.erb

This will style the email so put something like the following. This essentially routes to the contact_email method in contact_mailer.rb

1
2
3
4
5
6
7
8
9
10
11
Name:
 
<%= @email.name %>
 
Email: 
 
<%= @email.email_address %>
 
Message:
 
<%= @email.message %>

Now you can create a controller and use the form. Here’s an example using a static based site. The key is line #14 (ContactMailer.deliver_contact_email(@email)). This uses the contact_email method we created, and rails knows that if you put deliver_ in front of it, then it will deliver it according to your email setup (which you shoud have done already here).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# static controler
class StaticController < ApplicationController
  def index
  end
 
  def overview
  end
 
  def faqs    
  end
 
  def contact
    if request.post?
      @email = Email.new(params[:email])
      if @email.valid?
        ContactMailer.deliver_contact_email(@email)
        flash[:notice] = "Your email was sent successfully. Thanks."
        # without the redirect the params[:email] gets filled back into the form making it look like the form did not get submitted
        redirect_to :action => 'contact'
      end
    end 
  end
 
  def signup    
  end
 
 
end

And here’s what the contact.html.erb looks like associated with that action.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
<h1>Contact Us</h1>
 
<p>Email us.</p>
 
<% form_tag( {:action => "contact"}, :id => 'contact') do %>
<%= error_messages_for('email') %>
<ul>
	<li>
		<label for="email_name">Your Name</label>
		<%= text_field "email", "name", :class => 'input-txt' %>
	</li>
	<li>
		<label for="email_company">Your Email Address</label>
		<%= text_field "email", "email_address", :class => 'input-txt' %>
	</li>
 
	<li>
		<label for="email_message">Message</label>
		<%= text_area "email", "message", :cols => 10 %>
	</li>
	<li class="actn">
		<!-- <a id="sendmessage" href="">Send Message</a> -->
		<input type="submit" value="Send Message" />
	</li>
<% end %>

Finally, make sure to restart your local server using script/server before testing. Otherwise, the plugin active_form won’t load the first time.

Comments