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 " <<
current_associate.nice_to_s << " please update your whereabouts or
tell us what you’re doing:” %>
<%= 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,
On Sun, 2009-07-12 at 07:36 -0700, JannaB wrote:
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
On Sun, 2009-07-12 at 10:09 -0700, JannaB wrote:
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
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:
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>
<p>-Janna B</p>
On Sun, 2009-07-12 at 12:29 -0700, JannaB wrote:
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
Many Thanks Bill.
First, here is what shows in firebug for that partial which creates a
form:
Hello
Janna
please update your whereabouts || tell us what you're doing:
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>
<p>So far so good. Now, I update the text field, submit, and look at it<br>
again in firebug:</p>
<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:
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>
<p>Notice that the value for the message has changed in the textfield, of<br>
course, but the action I am trying to call<br>
channels.speakassfromwhereaboutspartial, doesn’t get called, because<br>
in it I have a puts statement:</p>
<p>def speakassfromwhereaboutspartial<br>
puts “speakassfromwhereaboutspartial”<br>
boxchangedata<br>
puts params[‘associate_inoutexplanation’]<br>
offToServer 0,current_associate.username,<br>
current_associate.username, "<span class="mention">@speakass</span><br>
"+current_associate.channelselectedassoc,:channelnotes => params<br>
[‘associate_inoutexplanation’]<br>
end</p>
<p>So it appears that somehow my observe_field is not observing – or<br>
likely not observing what I want it to! -Janna</p>
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
Hello
Ralph V.
please update your whereabouts || tell us what you're doing:
//<![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>
<pre><code> </div></code></pre>
Janna,
On Sun, 2009-07-12 at 19:11 -0700, JannaB wrote:
Below is the html created…can you see why the
observer is not passing on to /channels/
speakassfromwhereaboutspartial ? -Janna B
Not right off I can’t. What does Firebug tell you it’s doing when you
change the text? Do you see the request happen? You should. Do you
see any errors? Have you made triple-sure you don’t have a duplicate id
in your page?
Bill
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/
On Sun, 2009-07-12 at 21:28 -0700, JannaB wrote:
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).
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.