4

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.

Community
  • 1
  • 1
andi
  • 14,322
  • 9
  • 47
  • 46
  • Rails 3 supports tableless models - http://stackoverflow.com/questions/5118203/how-do-i-validate-a-non-model-form-in-rails-3 – Nitrodist Mar 13 '12 at 23:56

1 Answers1

2

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:

Rails model without database

ActiveRecord::Base Without Table

Community
  • 1
  • 1
hobodave
  • 28,925
  • 4
  • 72
  • 77
  • 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 Jul 28 '09 at 08:11
  • @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 Jul 28 '09 at 08:19
  • 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 Jul 28 '09 at 08:30
  • After a bit of a struggle (Validatable does not support I18n as ActiveRecord validations do) I got it to work. Thanks. – andi Jul 30 '09 at 11:33