problems with observe_field

Rolando Abarca <funkaster@...> writes:

Bill Walton wrote: > Because the observer is a Rails helper method. It's 'mission' is to > generate the Ajax code for you. But it can only to that with objects it > 'knows about.' By choosing not to use the Rails select method, you > chose > not to let Rails know about the object you asked it to observe. You > might > as well have asked it to observe a <p>. Rails can only help you if you > let > it.

mmm... but the first parameter to the observer_field method is a DOM id... so I thought I could use any valid DOM id...

> hth, > Bill

regards, rolando.-

I had the same misconception that Rolando did -- I created my own checkbox field using HTML, with a unique name and id, and tried to use observe_field to observe it by referencing its id. I thought the first parameter to observe_field was the DOM id of the thing I wanted it to observe.

Instead I'm trying to use rails to generate the checkbox for me, but I'm still getting the "this.callback is not a function" error in my Javascript console:

    <%= check_box_tag("private_check", :value => "1", :checked =>       true) %>Private     <%= observe_field 'private_check',       :frequency => 0,       :update => "private_span",       :url => {:action => 'mark_private', :only_path => false},       :with => "'p=' + escape(value)" %>

Any ideas what I'm doing wrong? Does the observe_field only work with real form elements and not ones created using the _tag methods?

Thanks, --Tessa

Tessa Lau wrote:

Instead I'm trying to use rails to generate the checkbox for me, but I'm still getting the "this.callback is not a function" error in my Javascript console:

   <%= check_box_tag("private_check", :value => "1", :checked => true) %>Private    <%= observe_field 'private_check',      :frequency => 0,

You don't need the :frequency option. The observer will fire when the check box changes state. I'd get rid of it.

     :update => "private_span",      :url => {:action => 'mark_private', :only_path => false},

Is there some reason you're setting :only_path to give you the fully qualified path? It's probably not the problem. Just wondered what situation is driving it's use.

     :with => "'p=' + escape(value)" %>

The purpose of the :with option in the case of observe_field is simply to provide the name for the key in the params hash the value in the field will be passed with. So if, in your controller action, you want to say something like: this_item = params[:p] then in your observe_field you'd use :with => 'p' You can, of course, use a more descriptive key.

Any ideas what I'm doing wrong?

My first guess would be that something about the syntax, maybe the order of your parameters, could be causing the problem. I've never had the error you're getting but the documentation says:

Required options are either of:       :url: url_for-style options for the action to call when the field has changed.       :function: Instead of making a remote call to a URL, you can specify a function to be called instead.

The error you're getting say's Rails thinks you're trying to tell it to call a function. I always put the url: => {} right after the field I'm watching. It's a required field. The other stuff like :frequency and :with is optional.

Does the observe_field only work with real form elements and not ones created using the _tag methods?

I use it with the _tag methods.

hth, Bill

Bill Walton <bill.walton@...> writes:

You don't need the :frequency option. The observer will fire when the check box changes state. I'd get rid of it. ... Is there some reason you're setting :only_path to give you the fully qualified path? It's probably not the problem. Just wondered what situation is driving it's use.

I was copying an example from the "Agile Web Development with Rails" book. After I got it to work I was planning to remove the things that aren't necessary, but when just starting out I've found it's always best to try to copy the working example as closely as possible!

The error you're getting say's Rails thinks you're trying to tell it to call a function. I always put the url: => {} right after the field I'm watching. It's a required field. The other stuff like :frequency and :with is optional.

Thanks! After playing around with removing various options, I discovered that removing the :frequency => 0 parameter causes it to work without an error.

That's confusing, because in the documentation for observe_field it says:

:frequency: The frequency (in seconds) at which changes to this field will be detected. Not setting this option at all or to a value equal to or less than zero will use event based observation instead of time based observation.

From that description I believed that setting :frequency => 0 would be equivalent to not setting :frequency at all, but that's not the case. Perhaps it's a bug in rails? I have rails v1.1.6.

Anyway, I have it working now. Thanks for the help!

--Tessa