# Validating Non-ActiveRecord Objects

**URL:** <https://www.ruby-forum.com/t/validating-non-activerecord-objects/114531>\
**Category:** Rails\
**Created:** [September 14, 2007, 6:30am UTC](https://www.ruby-forum.com/t/validating-non-activerecord-objects/114531 "2007-09-14T06:30:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [September 14, 2007, 6:30am UTC](https://www.ruby-forum.com/t/validating-non-activerecord-objects/114531/1 "2007-09-14T06:30:07Z")

</div>

I am trying to validate a form that’s not using a database.

The class (validateable.rb) comes from Rails Recipes and I am not sure  
how I can get the validation to work with my controller and view.

It’s probably trivial problem but I new to RoR as well as Ruby.

## /lib/validateable.rb from rails recipes

module Validateable  
[:save, :save!, :update\_attribute].each{|attr| define\_method(attr){}}  
def method\_missing(symbol, _params)  
if(symbol.to\_s =~ /(._)\_before\_type\_cast$/)  
send($1)  
end  
end

```
def self.append_features(base)
  super
    base.send(:include, ActiveRecord::Validations)
end

```

end

## /app/controllers/app\_controller.rb

class AppController \< ApplicationController

def submit

```
 if request.post?
    ContactMailer.deliver_contact_message(params[:name],

```

params[:email], params[:message])  
flash[:notice] = “Message sent!”  
redirect\_to :action =\> ‘contact’  
end  
end

## /app/views/contact\_mailer/contact.rhtml

# Contact Page
\<%= render :partial =\> 'navigation' %\>
* * *

\<% if flash[:notice] -%\>

\<%= flash[:notice] %\>
  
\<% end -%\>

\<%= error\_messages\_for “contact” %\>

\<%= start\_form\_tag(:action =\> ‘submit’) %\>

```
<p>
<label for="contact_name">Name</label> <br />
<%= text_field("contact", "name") %>
</p>

<p>
<label for="contact_email">E-mail</label> <br />
<%= text_field("contact", "email") %>
</p>

<p>
<label for="contact_message">Message</label> <br />
<%= text_area("contact", "message") %>
</p>

<%= submit_tag("Send") %>

```

\<%= end\_form\_tag %\>

## /app/models/contact\_mailer.rb

class ContactMailer \< ActionMailer::Base  
def contact\_message(cust\_name, cust\_email, cust\_message)  
@subject = “Hi”  
@body = “(#{cust\_message})”  
@recipients = cust\_email  
@from = “removed\_email\_address@domain.invalid”  
@sent\_on = Time.now  
end  
end

## #this is the _problem_ - how do I get this to work with my controller? /app/models/contact.rb

class Contact  
include Validateable  
attr\_accessor :email  
validates\_presence\_of :email  
end

Thank you

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [September 14, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/validating-non-activerecord-objects/114531/2 "2007-09-14T18:04:02Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/validating-non-activerecord-objects/114531/3 "2007-09-17T13:23:37Z")

</div>

Hi,  
I’d check  
[http://agilewebdevelopment.com/plugins/activerecord\_base\_without\_table](http://agilewebdevelopment.com/plugins/activerecord_base_without_table)  
to see if it helps. What if your Contact class is regular active record  
object (with validations, callbacks etc) but without database table  
beneath?

Pavlo

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [September 22, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/validating-non-activerecord-objects/114531/4 "2007-09-22T23:17:42Z")

</div>

So what happens when you call some\_contact.valid?

Fred

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [September 22, 2007, 5:49am UTC](https://www.ruby-forum.com/t/validating-non-activerecord-objects/114531/5 "2007-09-22T05:49:39Z")

</div>

Pavel Lysov wrote:

> Hi,  
> I’d check  
> [http://agilewebdevelopment.com/plugins/activerecord\_base\_without\_table](http://agilewebdevelopment.com/plugins/activerecord_base_without_table)  
> to see if it helps. What if your Contact class is regular active record  
> object (with validations, callbacks etc) but without database table  
> beneath?
> 
> Pavlo

Pavlo,

I tried that approach. However, I prefer to use the above solution. Do  
you have idea what might be wrong?

M

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [October 3, 2007, 3:16am UTC](https://www.ruby-forum.com/t/validating-non-activerecord-objects/114531/6 "2007-10-03T03:16:34Z")

</div>

Frederick C. wrote:

> So what happens when you call some\_contact.valid?
> 
> Fred

Frederic,

In IRB it works but I cannot get the error messages to show up. Any  
idea?

MJ
