Validating Non-ActiveRecord Objects

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 = “[email protected]
@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

Anyone?

Hi,
I’d check
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

So what happens when you call some_contact.valid?

Fred

Pavel Lysov wrote:

Hi,
I’d check
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

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