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
On Oct 22, 9:27 pm, Mozmonkey
<JeremyMail...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---