Topic: RJS and Observe_field


I had a quick question concerning RJS and Observe_fields.  Currently, if I use link_to_remote to link to my 'add' action, which has an add.rjs:

page.insert_html :bottom, 'testdiv',

This works fine.

Now if I have an observe_field linking to the same 'add' action, I get the following error:

try { new Insertion.Bottom("testdiv", "Hello"); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('new Insertion.Bottom(\"testdiv\", \"Hello\");'); throw e }

Any suggestions as to why this doesn't work?

Thanks for all your help!

Last edited by nekapoor (2007-04-18 01:05:16)

Re: RJS and Observe_field

What you posted, while a strange line of code, isn't actually an error message.  That's the javascript that your RJS was turned into.  The whole try {} catch(e) {} stuff is just like Ruby doing 'begin' and 'rescue'.

What should be happening is that this string of code should be evaluated and run as Javascript by the browser.  Is it getting printed directly into the page somehow?

Re: RJS and Observe_field

Yup, it directly prints out on the page in my 'testdiv' section.

Re: RJS and Observe_field

And also it replaces whatever is in the 'testdiv' section.  For example, if the observe_field is activated again, and thus going to the add.rjs file, it does not add that error message to the bottom of the 'testdiv' section.  It seems to replace whatever is in the 'testdiv' section.

Thanks again for your help.

Re: RJS and Observe_field

Try removing the :update => 'testdiv' part of your link_to_remote call.  does it work then?

Re: RJS and Observe_field

This is all I have in my link_to_remote:

<%= link_to_remote("Add",
          :url =>{ :action => :add }) %>

Did you mean try removing it from my add.rjs?

Thanks again

Re: RJS and Observe_field

Hmmm, I was hoping to get luck with that one :-)

I'm going to assume that there's no 'add' action in your controller.  That the RJS is executed directly without being called by some kind of render :action => :add or anything.
If that's the case then things should be working better than this.  The first step toward debugging this would be to install Firebug - the Firefox debugger.  It's an add-on that allows you to see the request happening in real-time and you can inspect parts of it.  It'll help you see if any of the following are happening:
-the content-type header is text/html (should be text/js or something like it)
-the RJS is actually executing but it's nested somehow.  Like page.insert_html :bottom, :test_div, "#{page.insert_html :bottom, :test_div, "hello"}"
-some other crazy thing is happening.

Tell me if that helps at all (or if it doesn't).

Re: RJS and Observe_field


I tried using Firebug and this is the output that I get when I click the checkbox to activate the observe_field:

Response Headers
Connection    close
Date    Sat, 21 Apr 2007 01:45:04 GMT
Set-Cookie    _TheTopPot_session_id=d1b101db0d7b8d1d21b11a05dc3a8279; path=/
Status    200 OK
Cache-Control    no-cache
Server    Mongrel
Content-Type    text/javascript; charset=utf-8
Content-Length    161
Request Headers
Host    localhost:3000
User-Agent    Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv: Gecko/20070309 Firefox/
Accept    text/javascript, text/html, application/xml, text/xml, */*
Accept-Language    en-us,en;q=0.5
Accept-Encoding    gzip,deflate
Accept-Charset    ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive    300
Connection    keep-alive
X-Requested-With    XMLHttpRequest
X-Prototype-Version    1.5.0
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Referer    http://localhost:3000/xmlfeed
Content-Length    2
Cookie    _TheTopPot_session_id=d1b101db0d7b8d1d21b11a05dc3a8279
Pragma    no-cache
Cache-Control    no-cache
try {

new Insertion.Bottom("bets", "Hello");

} catch (e) { alert('RJS error:\n\n' + e.toString()); alert('new Insertion.Bottom(\"bets\", \"Hello\"

);'); throw e }


Please note that 'bets' is the name of the div where I am trying to get the team to show up.  Also, I do have an 'add' action in my controller.

Any other suggestions?

Thanks again for all your help!

Re: RJS and Observe_field

Instead of using observe_field, try overriding the onclick attribute of the checkbox:

<%= check_box_tag :foo, 1, false, :onclick => remote_function(:url =>{ :action => :add }) %>

If that doesn't work, please post the code you are using.

Railscasts - Free Ruby on Rails Screencasts

Re: RJS and Observe_field

Hi Ryan,

Thank you very much for your suggestion.  I tried using check_box_tag but I'm having a problem passing in the id of the check_box_tag.  For example, I currently have this code in my view:

<%= check_box_tag 'test', 1, false, :onclick => remote_function(:url =>{ :action => :addtest }) %>

Now in my controller, I have:

def addtest
  @id = params[:id]

And then in my RJS:

page.insert_html :bottom, 'bets',

Using Firebug to debug, @id is blank, so nothing is being written to bets.  Any suggestions?

Thanks again for all your help!

Re: RJS and Observe_field

You can add the ID to the url and Rails will automatically pass that into params[:id]:

<%= check_box_tag 'test', 1, false, :onclick => remote_function(:url =>{ :action => :addtest, :id => @id }) %>

Last edited by Michael Boutros (2007-05-05 19:04:45)

Re: RJS and Observe_field

Thank you so much!  I have been quite frusturated trying to get this to work for a couple of weeks, didn't realize it was so simple smile

Thanks again!

Re: RJS and Observe_field

1. <%= link_to_remote("Add",
   2.           :url =>{ :action => :add }) %>

add parameter
:html => "div_id"