# Easy way to handle form input without a model class?

**URL:** <https://www.ruby-forum.com/t/easy-way-to-handle-form-input-without-a-model-class/95674>\
**Category:** Rails\
**Created:** [January 19, 2006, 8:53am UTC](https://www.ruby-forum.com/t/easy-way-to-handle-form-input-without-a-model-class/95674 "2006-01-19T08:53:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [January 19, 2006, 8:53am UTC](https://www.ruby-forum.com/t/easy-way-to-handle-form-input-without-a-model-class/95674/1 "2006-01-19T08:53:44Z")

</div>

I have a couple forms that I’d like to be able to validate and  
automatically populate, but it shouldn’t be based on AR. In fact I  
often have a bunch of small forms that I can’t really justify writing  
a whole new model class for anyway. I’d like to validate the form  
input, and then use rails helpers to automatically populate the form  
if validations fail. I’ve already found some links on validating  
without subclassing from AR, but nothing about auto populating forms  
without a model, with or without rails helpers.

If there’s no existing way to do this, I already have an idea for  
something simple. I’d write a class that takes a hash at  
initialization to populate instance variables and automatically add  
accessors, and use method\_missing to handle the object before it’s  
initialized with the hash. Pretty easy…but I just wanted to see if  
something like this exists already.

Thanks,  
Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [January 19, 2006, 9:38am UTC](https://www.ruby-forum.com/t/easy-way-to-handle-form-input-without-a-model-class/95674/2 "2006-01-19T09:38:52Z")

</div>

Alright here’s what I came up with. It’s a modification of Peter  
Donald’s ActiveForm  
([http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects](http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects)).  
The only real differences here are the initialize method, which sets  
the attributes, and the method\_missing, which returns the value of  
instance\_variable\_get (which is nil if the instance variable doesn’t  
exist). Because it returns nil, the form helpers don’t complain about  
a method not existing.

Hopefully some people can take a look at this and improve it a bit. I  
don’t really like how I’ve had to reimplement method\_missing…it does  
the same exact thing as #. Also some of the stuff in  
method\_missing may be extraneous - I just hacked up a quick thing that  
works for me. I appreciate any comments, and big thanks to Peter  
Donald for coming up with the original ActiveForm.

# Note “.valid?” method must occur on object for validates\_associated

class ActiveForm

def initialize(init\_values = {})  
init\_values.each{ |key, value| instance\_variable\_set(“@#{key}”,  
value) }  
end

def   
instance\_variable\_get(“@#{key}”)  
end

def method\_missing( method\_id, \*args )  
if md = /\_before\_type\_cast$/.match(method\_id.to\_s)  
attr\_name = md.pre\_match  
return self[attr\_name] if self.respond\_to?(attr\_name)  
end

```
instance_variable_get("@#{method_id.to_s}")
#super

```

end

protected  
def raise\_not\_implemented\_error(\*params)  
ValidatingModel.raise\_not\_implemented\_error(\*params)  
end

def self.human\_attribute\_name(attribute\_key\_name)  
attribute\_key\_name.humanize  
end

def new\_record?  
true  
end

# these methods must be defined before include

alias save raise\_not\_implemented\_error  
alias update\_attribute raise\_not\_implemented\_error

public  
include ActiveRecord::Validations

protected

# the following methods must be defined after include so that they

overide

# methods previously included

alias save! raise\_not\_implemented\_error

class \<\< self  
def raise\_not\_implemented\_error(\*params)  
raise NotImplementedError  
end

```
alias validates_uniqueness_of raise_not_implemented_error
alias create! raise_not_implemented_error
alias validate_on_create raise_not_implemented_error
alias validate_on_update raise_not_implemented_error
alias save_with_validation raise_not_implemented_error

```

end  
end

require ‘dispatcher’  
class Dispatcher  
class \<\< self  
if ! method\_defined?(:form\_original\_reset\_application!)  
alias :form\_original\_reset\_application! :reset\_application!  
def reset\_application!  
form\_original\_reset\_application!  
Dependencies.remove\_subclasses\_for(ActiveForm) if  
defined?(ActiveForm)  
end  
end  
end  
end
