views:

1221

answers:

1

Can anyone share a few guides, tips or links to what constitute best practices for creating a model that represents a contact form which is not backed up by a table in the database, but can still validate the data entered?

Thanks.

EDIT: I forgot to mention, that if it is a gem or a plug-in, it should work with Ruby 1.9.1 on Rails 2.3.2.

This - stackoverflow.com/questions/315850/rails-model-without-database doesn't really cut it, as it is a pretty old and not maintained and neither does this - stackoverflow.com/questions/937429/activerecordbase-without-table-rails as it is pretty incomplete.

+2  A: 

Hey andi.

Check out the validatable gem.

class Person
  include Validatable
  validates_presence_of :name
  attr_accessor :name
end

See: http://validatable.rubyforge.org/

See: http://rorblog.techcfl.com/2008/04/02/custom-form-validations-without-activerecord/

This has also been asked several times on StackOverflow:

http://stackoverflow.com/questions/315850/rails-model-without-database

http://stackoverflow.com/questions/937429/activerecordbase-without-table-rails

hobodave
Thanks for the Validatable gem. I'll check it out. As for the dupe, I have seen those questions, but the solution for both was to extend from Base and override methods, and I don't really think that is the best way here. Should I have mentioned that in the question in order to not get downvoted?
andi
@andi: That's usually a good rule of thumb. Otherwise it makes it seem like you were just lazy. I removed my downvote, thanks for the response.
hobodave
Yeah, you are probably right. I'm not lazy, but just looking for a good solution. Anyway, the validatable gem seems like it. I don't know *how* I didn't see it, as I have read this blog post - http://www.prestonlee.com/2007/12/29/rails-20-validations-without-extending-activerecordbase/182 - and it is posted in the comments as a better alternative. I'll try the gem and let you know. Thanks.
andi
After a bit of a struggle (Validatable does not support I18n as ActiveRecord validations do) I got it to work. Thanks.
andi