Problems with observe_field

Hi all, I’m having problems with an observe_field, this is the view:

<%= form_tag %>

Customer: Select a customer <%= options_for_select Cliente.find_all.collect {|c| [c.razon_social, c.id]} %>

<%= observe_field :cliente_id, :frequency => 0, :url => {:controller => 'visitas', :action => 'contactos_cliente'}, :update => 'contactos', :with => 'cliente_id' %>
<%= end_form_tag %>

but it’s not working… it throws a javascript error:

Error: this.callback is not a function
Archivo Fuente:
http://localhost:3000/javascripts/prototype.js?1151586486
Línea: 1625

(this is from Firefox)
I’ve included the :defaults javascript files…
Any idea what could be the problem?
thanks!!!

Rolando A. wrote:

Hi all, I’m having problems with an observe_field, this is the view:

but it’s not working… it throws a javascript error:

Error: this.callback is not a function

It would probably help if you’d include the details of the error it’s
throwing.

Best regards,
Bill

Bill W. wrote:

It would probably help if you’d include the details of the error it’s
throwing.

Best regards,
Bill

That’s all I have, it’s a javascript error. It’s not generating neither
a POST nor GET request to the server.
regards,
rolando.-

Hi Rolando,

Rolando A. wrote:

That’s all I have, it’s a javascript error. It’s not generating neither
a POST nor GET request to the server.

I just took another look at your code. Sorry I missed this yesterday
but I
think the problem’s in your . The format is way wrong. For
starters, it’s not enclosed in <%= %> so I’m pretty sure RoR will just
treat it as text to render to the page and will not generate a select
tag.
I think the error you’re getting is telling you something like “I don’t
observe things like text.” That would also be why no POST is occurring.
I’d recommend checking out the documentation on the select method at
http://api.rubyonrails.org for the proper syntax for select and
options_for_select because I’m pretty sure what you’ve got wrt options
isn’t
going to work either.

hth,
Bill

Rolando A. wrote:

select tag…
Yes. That will definitely work.
Don’t know why this works and not the observer…

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

. Rails can only help you if you
let
it.

hth,
Bill

Bill W. wrote:

I just took another look at your code. Sorry I missed this yesterday
but I
think the problem’s in your . The format is way wrong. For
starters, it’s not enclosed in <%= %> so I’m pretty sure RoR will just
treat it as text to render to the page and will not generate a select
tag.

I’m using a plain html tag, not the select method from
ActionView helpers…
Anyway, I did this and it’s working now:

I just hardcoded the Prototype Updater in the OnChange event of the
select tag…
Don’t know why this works and not the observer…

regards,
rolando.-

Bill W. 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

. 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.-

Rolando A. <funkaster@…> writes:

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

Bill W. <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 D. 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

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