observe_field on text_field

I've implemented observe_field succesfully on other controls, but
never on a text field. For whatever reason, the following partial
never executes the action specified in the url of my observe_field
below.

Does anyone see what I am doing wrong here and how I can make that
action fire? Thanks, Janna B.

<% form_for current_associate, :html => {:name => 'whereaboutsform'}
do |f| %>
   <%= f.label :inoutexplanation, "Hello <b>" <<
current_associate.nice_to_s << "</b> please update your whereabouts or
tell us what you're doing:" %><br />
   <%= f.text_field :inoutexplanation %>
   <%= f.submit "Update" , :class=>'button' %>
   <% if current_associate.channel_id != nil and
current_associate.channel_id > 0 %>
   <%= observe_field "associate_inoutexplanation", :url =>
{:controller => :channels, :action =>
'speakassfromwhereaboutspartial' }, :with =>
"'associate_inoutexplanation=' + value" %>
   <% end %>
<% end %>

Hi Janna,

I've implemented observe_field succesfully on other controls, but
never on a text field. For whatever reason, the following partial
never executes the action specified in the url of my observe_field
below.

Does anyone see what I am doing wrong here and how I can make that
action fire? Thanks, Janna B.

Two things ....

First, look at the html in Firebug to make sure the Ajax actually exists
on the page. You've got the observe_field wrapped in a condition and it
may be that it's not getting rendered.

Second, assuming the observer is getting rendered, it's looking for a
field that doesn't exist and so will never fire.

As side notes...

'and' and '&&' are not the same thing. Although it's probably not the
source of your problem here, you should be aware of operator precedence.
'and' and 'or' are at the very bottom of the list.

Idiom-wise 'current_associate.channel_id != nil' is more typically
written '!current_associate.channel_id.nil?'

It's safer to use .blank? than .nil? (or '!= nil') as it will catch nil,
false, and the empty string.

Lastly,
:with => "'associate_inoutexplanation=' + value"
is equivalent to
:with => "associate_inoutexplanation".

HTH
Bill

Bill,

Thank you for your pointers regarding Rails-like idioms, etc. Coming
from a Java background, I really want to break from that and do things
in the Rails manner, and I appreciate your pointing this out (things
like .blank, etc.)

As for my observer_field, I don;t follow you when you say the field
doesn;t exist and will never fire. The field with id of
'associate_inoutexplanation' does exist (what am I missing here in
what you are saying in this regard?) I have enclosed the relevant
portion of the firebug output which I have copied here:

<input id="associate_inoutexplanation" type="text" value="This is
Sunday #2" size="30" name="associate[inoutexplanation]"/>
<input id="associate_submit" class="button" type="submit"
value="Update" name="commit"/>
<script type="text/javascript">
1
2//<![CDATA[
3new Form.Element.EventObserver('associate_inoutexplanation', function
(element, value) {new Ajax.Request('/channels/
speakassfromwhereaboutspartial', {asynchronous:true, evalScripts:true,
parameters:'associate_inoutexplanation=' + value +
'&authenticity_token=' + encodeURIComponent('yE+PvOnafqUVEYfQqxhY/
ThAvtdW3zoyh+FkXfDLdNo=')})})
4//]]>
5
</script>
</form>

-Janna B

I can't find your original post at the moment but I obviously misread the form.
My apologies.

So what happens in the Firebug console when you change the text in the field?

Repost the code if you like and I'll take another, more careful look.

Best regards,
Bill

Many Thanks Bill.

First, here is what shows in firebug for that partial which creates a
form:

<div id="whereabouts">
<form id="edit_associate_1" class="edit_associate"
name="whereaboutsform" method="post" action="/associates/1">
<div style="margin: 0pt; padding: 0pt;">
<input type="hidden" value="put" name="_method"/>
<input type="hidden" value="yE+PvOnafqUVEYfQqxhY/ThAvtdW3zoyh
+FkXfDLdNo=" name="authenticity_token"/>
</div>
<label for="associate_inoutexplanation">
Hello
<b>Janna</b>
please update your whereabouts || tell us what you're doing:
</label>
<br/>
<input id="associate_inoutexplanation" type="text" value="This is
Sunday msg #1 " size="30" name="associate[inoutexplanation]"/>
<input id="associate_submit" class="button" type="submit"
value="Update" name="commit"/>
<script type="text/javascript">
1
2//<![CDATA[
3new Form.Element.EventObserver('inoutexplanation', function(element,
value) {new Ajax.Request('/channels/speakassfromwhereaboutspartial',
{asynchronous:true, evalScripts:true,
parameters:'associate_inoutexplanation=' + value +
'&authenticity_token=' + encodeURIComponent('yE+PvOnafqUVEYfQqxhY/
ThAvtdW3zoyh+FkXfDLdNo=')})})
4//]]>
5
</script>
</form>
</div>

So far so good. Now, I update the text field, submit, and look at it
again in firebug:

<div id="whereabouts">
<form id="edit_associate_1" class="edit_associate"
name="whereaboutsform" method="post" action="/associates/1">
<div style="margin: 0pt; padding: 0pt;">
<input type="hidden" value="put" name="_method"/>
<input type="hidden" value="yE+PvOnafqUVEYfQqxhY/ThAvtdW3zoyh
+FkXfDLdNo=" name="authenticity_token"/>
</div>
<label for="associate_inoutexplanation">
Hello
<b>Janna</b>
please update your whereabouts || tell us what you're doing:
</label>
<br/>
<input id="associate_inoutexplanation" type="text" value="This is
Sunday msg #2" size="30" name="associate[inoutexplanation]"/>
<input id="associate_submit" class="button" type="submit"
value="Update" name="commit"/>
<script type="text/javascript">
1
2//<![CDATA[
3new Form.Element.EventObserver('inoutexplanation', function(element,
value) {new Ajax.Request('/channels/speakassfromwhereaboutspartial',
{asynchronous:true, evalScripts:true,
parameters:'associate_inoutexplanation=' + value +
'&authenticity_token=' + encodeURIComponent('yE+PvOnafqUVEYfQqxhY/
ThAvtdW3zoyh+FkXfDLdNo=')})})
4//]]>
5
</script>
</form>
</div>

Notice that the value for the message has changed in the textfield, of
course, but the action I am trying to call
channels.speakassfromwhereaboutspartial, doesn't get called, because
in it I have a puts statement:

  def speakassfromwhereaboutspartial
    puts "speakassfromwhereaboutspartial"
    boxchangedata
    puts params['associate_inoutexplanation']
   offToServer 0,current_associate.username,
current_associate.username, "@speakass
"+current_associate.channelselectedassoc,:channelnotes => params
['associate_inoutexplanation']
  end

So it appears that somehow my observe_field is not observing -- or
likely not observing what I want it to! -Janna

Many Thanks Bill.

You're very welcome.

<input id="associate_inoutexplanation"
Form.Element.EventObserver('inoutexplanation'

<input id="associate_inoutexplanation"
Form.Element.EventObserver('inoutexplanation'

Notice that we're right back where we started from. The id of the
element you say you want to observe is not the id of the element you
are, in fact, watching...

Just a wild hunch, but I'd bet a nickle that you a) have more than one
version of your app on your system, and b) don't realize that the
directory you're in when you start your web server is the app you're
running. Close?

If not, it would help a lot if you would explain the difference between
the above and the code you showed earlier that made me think I must have
misread your original post.

Best regards,
Bill

Bill,

Sorry, it;s because I wrote the last post while playing around with it
trying to experiment (I really only have one version on this
system).....BUT, "associate_inoutexplanation" is the id of the text
field I am trying to observe, as well as what I put into my
observe_field to observe it. The partial itself creates the following
html where you can see the associate_inoutexplanation as both the
input field's id and the javascript created by my
observe_field "associate_inoutexplanation", :url => {:controller
=> ...
in my partial. Below is the html created.....can you see why the
observer is not passing on to /channels/
speakassfromwhereaboutspartial ? -Janna B

<div id="whereabouts">
         <form action="/associates/1" class="edit_associate"
id="edit_associate_1" method="post" name="whereaboutsform"><div
style="margin:0;padding:0"><input name="_method" type="hidden"
value="put" /><input name="authenticity_token" type="hidden"
value="xgorbWtiTdb15yhJ9il0TbbD1szBHSAcWb7p6GVffF0=" /></div>
   <label for="associate_inoutexplanation">Hello <b>Ralph Vince</b>
please update your whereabouts || tell us what you're doing:</

<br />

   <input id="associate_inoutexplanation" name="associate
[inoutexplanation]" size="30" type="text" value="This is Sunday msg
#2" />
   <input class="button" id="associate_submit" name="commit"
type="submit" value="Update" />
   <script type="text/javascript">
//<![CDATA[
new Form.Element.EventObserver('associate_inoutexplanation', function
(element, value) {new Ajax.Request('/channels/
speakassfromwhereaboutspartial', {asynchronous:true, evalScripts:true,
parameters:'associate_inoutexplanation=' + value +
'&authenticity_token=' + encodeURIComponent
('xgorbWtiTdb15yhJ9il0TbbD1szBHSAcWb7p6GVffF0=')})})
//]]>
</script>

</form>

         </div>

Janna,

I dont see anything in firebug that shows me what happens on a get or
post -- just the static snapshot of the page in a nice, expandable
tree view. HOWEVER, I do notice in my mongrel monitor window when I
submit the form I get the following (I have NO idea what this means!
But I am certain it is at the root of my problems):

Mon Jul 13 00:26:25 -0400 2009: Read error: #<Errno::ECONNABORTED: An
establishe
d connection was aborted by the software in your host machine.>
G:/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/
bin/../lib/
mongrel/http_response.rb:137:in `write'
G:/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/
bin/../lib/
mongrel/http_response.rb:137:in `write'
G:/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/
bin/../lib/
mongrel/http_response.rb:95:in `send_header'
G:/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/
bin/../lib/
mongrel/http_response.rb:146:in `finished'
G:/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/
bin/../lib/
mongrel.rb:165:in `process_client'
G:/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/
bin/../lib/
mongrel.rb:285:in `run'
G:/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/
bin/../lib/
mongrel.rb:285:in `initialize'
G:/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/
bin/../lib/
mongrel.rb:285:in `new'
G:/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/
bin/../lib/

And when I get rid of my observe_field in my partial...this error goes
away, so clearly the error is related to the js created by
observe_field (even if by my not getting good parameters into it).

So now I just need to figure out an alternative to observe_field. It's
a pity ruby doesn have an observe field in a db. There must be a gem
for that? If I can;t get observe_field to work on a simple text box, I
need to find an alternative way of doing this.

I dont see anything in firebug that shows me what happens on a get or
post -- just the static snapshot of the page in a nice, expandable
tree view.

Firebug has several tabs. The one that shows you the tree you're
looking at is the HTML tab. The Console tab shows you the POSTs and
GETs. Change to the Console tab and let us know what you see.

HOWEVER, I do notice in my mongrel monitor window when I
submit the form I get the following (I have NO idea what this means!
But I am certain it is at the root of my problems):

Mon Jul 13 00:26:25 -0400 2009: Read error: #<Errno::ECONNABORTED: An
established connection was aborted by the software in your host machine.>

It means that in the process of constructing the response, Mongrel lost
a connection it needs. Typically it's the connection to the database
though, in this case it looks like it's the connection to the browser.
You can see below that it's in the process of responding. (read error
stacks from the bottom up).