Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RoR and Ajax

 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Lasse that helps.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic