Using observe_form

I’ve tried to use observe_form several times in the past but never had much success. I finally spent the time to investigate and figure out why.

The documentation for observer_form is pretty sparse. It says:

Like observe_field, but operates on an entire form identified by the DOM ID form_id. options are the same as observe_field, except the default value of the :with option evaluates to the serialized (request string) value of the form.

It sounds to me like I should be able to do:

observe_form(  'search_form',
    	          :frequency  =>  1,
    	          :url        =>  { :action =>  :search })

If you do that, it doesn’t submit any form values. With observe_field, you either need to specify :with=>'value' or use the :update flag. I’ve played around with a ton of options and never got anything to actually submit a full and complete form.

After looking at the source code, I found what I was missing. I needed to provide the submit option to specify the form to submit:

observe_form(  'search_form',
    	          :frequency  =>  1,
    	          :url        =>  { :action =>  :search },
                  :submit     =>  'search_form')

That’s the great thing about open source software. If you can’t figure out the solution to your problem from the documentation, you can try to figure it out using the code.