wood burning stoves 2.0*
The moose likes Other Languages and the fly likes RoR and Ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "RoR and Ajax" Watch "RoR and Ajax" New topic
Author

RoR and Ajax

Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Request some inputs on the way RoR does Ajax. Please use simple example code ( not heavy duty RoR/ Ruby ). Is the response DOM constructed on the server?
Does it use 'innerHTML' to render on the client? What javascript library is the ajax support based on?

Say I have a trivial dropdown

Country <select id="country">
<option>India</option>
<option>USA</option>
<option>UK</option>
</select><br/>

and i need to update another DD based on the country you picked,

State/County<select id="state">
<option>California</option>
<option>Dallas</option>
</select>

How do you indicate on the 'country' DD that you need to do something on 'onchange' meaning which method on the controller is responsible for generating the response. How is the error handling done in case of any issues in making an ajax request/getting a response etc.

How is the response constructed on the server and how does the response look like?

thanks!
[ February 10, 2006: Message edited by: Karthik Guru ]
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Karthik, While i understand you are looking for specific example, just thought i would post this link (http://www.rubyonrails.com/screencasts) which has link to source as well.


Spritle Software Blogs
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Originally posted by Balaji Loganathan:
Karthik, While i understand you are looking for specific example, just thought i would post this link (http://www.rubyonrails.com/screencasts) which has link to source as well.


Thanks for the link. I checked out the ajax-rails presentation but i guess this is not the right pace for me. It appeared as if the presenter was in a hurry to get somewhere.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Karthik Guru:
Request some inputs on the way RoR does Ajax.

This article at ONLamp.com might be a good start.

Originally posted by Karthik Guru:
Is the response DOM constructed on the server?

Well, in general it's the raw snippet of HTML that gets constructed on the server but of course you could build that HTML snippet with some DOM library or by string concatenation--it's up to you to decide.

Originally posted by Karthik Guru:
Does it use 'innerHTML' to render on the client?

Yes, it uses "innerHTML" as far as I can tell.

Originally posted by Karthik Guru:
What javascript library is the ajax support based on?

Prototype

Originally posted by Karthik Guru:
Say I have a trivial dropdown

and i need to update another DD based on the country you picked,

How do you indicate on the 'country' DD that you need to do something on 'onchange' meaning which method on the controller is responsible for generating the response. How is the error handling done in case of any issues in making an ajax request/getting a response etc.

You could try something like this (based on an example from the ONLamp.com article):


Originally posted by Karthik Guru:
How is the response constructed on the server and how does the response look like?

In the above example, the Ajax request would be handled by the "get_states_for_country" action in the "karthik_controller", which would be responsible for rendering back a fragment of HTML that the client-side JavaScript then dumps inside the element identified with ":update => :element_id", e.g.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Oh, and I haven't actually used Ajax in Rails to update the contents of a non-DIV element so that snippet right there comes without a warranty
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
thanks Lasse that helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RoR and Ajax
 
Similar Threads
Dynamic Jsp coding
Test/Debug AJAX?
Ajax Autocomplete example in servlet and jsp
Ajax, Struts, and a few pesky event based issues
2 Drop Down JSF: AJAX: hx:ajaxRefreshRequest