18

Let's say I have a search form and I want to validate the length of the keyword. I want the form to highlight the invalid field (like model forms). I've looked everywhere and can't seem to find any useful information on validating non-model-backed forms in Rails 3. I've tried the validatable gem, but this doesn't seem to work with Rails 3:

undefined method `model_name' for Login:Class.

Any ideas?

JW8
  • 1,496
  • 5
  • 21
  • 36
Mike
  • 335
  • 1
  • 5
  • 10

2 Answers2

20

With Rails 3 you can extend "plain old Ruby objects" with some ActiveModel extensions like Validations etc. ActiveRecord is extension of ActiveModel with DB support. Here is a very good Railscast describing it with easy example:

In Rails 3 the non-database functionality of Active Record is extracted out into Active Model. This allows you to cleanly add validations and other features to tableless models. http://railscasts.com/episodes/219-active-model

gertas
  • 16,869
  • 1
  • 76
  • 58
  • 1
    Extending POROs would apply if the question wanted non-ActiveRecord forms... what about **non-model**, like a search form, or a couple fields letting a user pick a date range... anyone? – fakeleft Jan 19 '15 at 13:55
3

You can try to use tableless model. Check out here for more infomation: http://railscasts.com/episodes/193-tableless-model

fl00r
  • 82,987
  • 33
  • 217
  • 237