# Using a hash with a FormHelper

**URL:** <https://www.ruby-forum.com/t/using-a-hash-with-a-formhelper/118013>\
**Category:** Rails\
**Created:** [October 23, 2007, 11:04am UTC](https://www.ruby-forum.com/t/using-a-hash-with-a-formhelper/118013 "2007-10-23T11:04:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [October 23, 2007, 11:04am UTC](https://www.ruby-forum.com/t/using-a-hash-with-a-formhelper/118013/1 "2007-10-23T11:04:40Z")

</div>

Is it possible to have a FormHelper (i.e. text\_field) get it’s value  
from a hash, instead of an object. For example, my application uses a  
datetime field to determine when something should happen. In the  
database it is stored as YYYY-MM-DD hh:mm:ss (20071022 19:20:00), but  
for the user I want to format it like MM/DD/YYYY hh:mm and using  
standard time instead of military (10/22/2007 7:20p). My hash looks  
something like this:

@date = { :year =\> 2007, :month =\> 22, :day =\> 10, :hour =\> 7, :minute  
=\> 20, :ampm =\> “pm” }

Is there anyway I can use a FormHelper for this and similar hashes:

\<%= text\_field @date, :year %\>

There are a number of areas on the site where I have a similar issue  
and would like to find a consistent solution.

Thanks,  
Jeremy

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [October 23, 2007, 11:08am UTC](https://www.ruby-forum.com/t/using-a-hash-with-a-formhelper/118013/2 "2007-10-23T11:08:47Z")

</div>

To solve this, I created a class that takes a hash as it’s input and  
use the resulting object in my FormHelper functions. I am not sure  
what the hit on performance is on this, but it works!

Greg

module HashModel

class Model

```
def initialize(hash)
  if /^Hash/.match(hash.class.to_s)
    @my_hash = hash
  else
    raise "Only a hash may be passed to object. You passed:

```

#{hash.class.to\_s}"  
end  
end

```
def empty?
  @my_hash.empty?
end

def method_missing(method_id)

  # if value is an number, then we should return an int. This is

```

for the select helper. “1” and 1 are not the same, so the selected  
element does not get chosen.  
if @my\_hash && @my\_hash.key?(method\_id.to\_s) && /\A\d+  
\Z/.match(@my\_hash[method\_id.to\_s])  
@my\_hash[method\_id.to\_s].to\_i  
elsif @my\_hash && @my\_hash.key?(method\_id.to\_s)  
@my\_hash[method\_id.to\_s]  
else  
nil  
end  
end  
end  
end
