Well, it turns out that with a little bit of work, you can. Based on an idea I found at http://pullmonkey.com/2008/1/6/convert-a-ruby-hash-into-a-class-object/, I created a simple Object which wraps a Hash and behaves like an ActiveRecord. This class looks like:
class HashObjectThen to make it easy to use, I put a helper function in helper/application_helper.rbdef initialize(hash={}) @hash = hash end def method_missing(sym, *args, &block) @hash[sym] end end
def form_for_hash(hash, name, url, html_options={}, &proc)Now I can use it in the view as such: (e.g. new.html.haml)object = HashObject.new(hash) form_for object, :as=>name, :url=>url, :html=>html_options, &proc end
= form_for_hash @person, :person, special_people_path do = f.label :first_name, 'First Name' = f.text_field :first_name = f.label :last_name, 'Last Name' = f.text_field :last_name %br = f.label :phone_number, 'Phone Number' = f.text_field :phone_number -# etc etc etcThen in your SpecialPeopleController you would have methods like
def newAnd that is all you need to do to be able to use a Hash to populate your forms.@person = {:first_name=>'Jenny', :phone_number=>'867-5309'} end def create @person = params[:person] # logic for populating/updating models based on hash goes here end
2 comments:
Does the madking take requests for topics? Can we have a thread on say parallel programming? Has the madking ever done parallel programming? What was the most difficult part? What software did the madking use? java+jppf?
-Alan
Is there a reason you'd do this rather than just using an OpenStruct?
Post a Comment