Validate your forms with a table-less model

Ive followed this example of how to validate a form with a tableless
model:

http://rails.techno-weenie.net/tip/2005/11/19/validate_your_forms_with_a_table_less_model

Ive got this example working but when validation fails the failed boxes
are not highlighted in red. At the moment i have a contact details form
and want to validate the information entered by the user but this will
just result in sending an email, its not saving to a database table.

My code in the controller is this at the moment and this works but if
the validation fails it just displays the flash message i have provided,
not highlighting the boxes in red.

def contact
@contact = Contact.new(params[:contact])
if @contact.valid?
OrderMailer::deliver_send_an_email() # notice the ‘deliver_’
prefix
flash[:notice] = ‘Query was successfully created.’
redirect_to :action => ‘contactus’
else
flash[:notice] = ‘Query was not successfully created.’
redirect_to :action => ‘contactus’
end
end

This my contacts model class:

class Contact < ActiveRecord::Base
def self.columns() @columns ||= []; end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s,
default, sql_type.to_s, null)
end
column :name, :string
column :contact_details, :string
column :subject, :string
column :query, :string
validates_presence_of :name, :contact_details, :query
end

Can anyone help me please?

I have a similar problem. Model validation seems to work fine, but my
error object is empty, so the errors don’t get displayed i dont know
why. I dont have any errors.

Pabloz wrote:

I have a similar problem. Model validation seems to work fine, but my
error object is empty, so the errors don’t get displayed i dont know
why. I dont have any errors.

ok, i got it…i used redirect instead of render. rookie mistake ( cause
i am a rookie :slight_smile: )

If you want to avoid cluttering your model definitions, you can use my
plugin:

http://svn.viney.net.nz/things/rails/plugins/active_record_base_without_table/
http://svn.viney.net.nz/things/rails/plugins/active_record_base_without_table
-Jonathan