Caution! - Many of these posts are creepy-old in the Ruby on Rails world (before 1.0!)
The :author => Charles Abbott now blogs here

observe_field - Ajax!

2006-04-21   [ 0 comments ]

Web 2.0 - or so it is called is the big buzzword these days. Web developers need to learn how to utilize ajax (asynchronous javascript and xml) to further evolve their websites, or face extinction. Thankfully Ruby on Rails embraces Ajax and makes developing truly 'alive' websites within a modest amount of key punching.

So - here we go, 2.0!

I initially decided to try out the function periodically_call_remote just because it sounded pretty cool. However, the API did not have an example of it in use, so I decided it best to start with something that did - thus enter:

observe_field(field_id, options = {})

Options:

  • :url - for the action to call when the field has changed
  • :function - instead of making a remote call to a URL, you can specify a function to be called

  • :frequecny - the frequency (in seconds) at which changes to this field will be detected
  • :update - specifies the DOM ID of the element whose innerHTML should be updated
  • :with - javaScript expression specifying the parameters for the XMLHttpRequest
  • :on - specifies which event handler to observe. default = "changed"

First, before you waste anytime let me forewarn you! I wasted nearly 30 minutes trying to get :function to work. :function is supposed to let you execute local javascript instead of making an AJAX request to some outside page. However, I had absolutely no luck with it so - for those that just skim the article:

I could not get the :function option to work!!!

Essentially all you need is the :url and :update options to get it working. The :url option tells the function where to make the request. The :update option tells the function where to put the results (presumably some <div> tag, or table with an id="sameasinupdateoption").

In my example below I am going to point my ajax request to an action in the same /user controller. And tell the results to go into a div tag. First my form and ajax method:

...start form tag...
<%= text_field 'thought', 'search' %>
..end form tag...

<%= observe_field 'thought_search', { :url => "http://www.forthecode.com/user/ajax", :update => "theupdate" } %>

The only thing I did not include in the above was the DIV tag - just be sure to have one with an id attribute = whatever you set the option :update => "" to.

So - the above sets up an active AJAX observer that watches the form field you supplied (be sure to check that your form field's id attribute is the same as you supplied - in this case my form field id is 'thought_search').

Great we have it setup, now when you call up your page you should notice in the html source something like so -


<script src="/javascripts/prototype.js" type="text/javascript"></script>
<script src="/javascripts/effects.js" type="text/javascript"></script>
<script src="/javascripts/dragdrop.js" type="text/javascript"></script>
<script src="/javascripts/controls.js" type="text/javascript"></script>

<script type="text/javascript">
//<![CDATA[
new ....your ajax method
//]]>
</script>

Don't fear - it isn't a hostile takeover of your page, instead it is Rails just pulling in a ton of helpful functions.

If you notice that your AJAX call is not doing anything check a couple of things:

Be certain ...

1) that your id values are all correct.

2) that the :url you are calling actually has a corresponding action AND view for the data to be pulled from.

Just remember that the ajax call is not pulling data straight from your controller - instead it must pull data from some other page or - you guessed it! - XML or similar file.

Well, I intended to go over the periodically_call_remote function but it will have to wait until the next post. However, you can witness it currently working on this page. Happy coding! =P

:author => "Charles Abbott"
Converting to Ruby on Rails
 

What?

Who?              Link?



Frameworks Good or Bad?   :date => "2007-10-06"
Where is ForTheCode.com Going?   :date => "2007-09-23"
Refactoring - Vital to Software Development   :date => "2007-09-23"
Mongrel Cluster a quick note - and extra notes   :date => "2007-05-20"
Linux Mongrel and Rails   :date => "2007-05-15"
form_remote_tag revisited   :date => "2007-01-07"
How To: Ubuntu 6.10 Edgy on Rails part 3   :date => "2006-12-30"
How To: Ubuntu 6.10 Edgy on Rails part 2   :date => "2006-12-24"
How To: Ubuntu 6.10 Edgy on Rails   :date => "2006-12-22"
verify ... 5.times do cycle   :date => "2006-09-25"
country_select, country_options_for_select, mail_to   :date => "2006-09-05"
Generate and Send Email in Rails   :date => "2006-08-26"
FDF Model, gsub, and send_data   :date => "2006-08-18"
Active Directory Authentication with acts_as_authenticated   :date => "2006-08-17"
Apache2 proxy with Lighttpd - FastCGI for Rails   :date => "2006-08-08"
reverse! && a simple file Upload Class   :date => "2006-07-29"
send_file - a link to download a file   :date => "2006-07-24"
Environments (production, development, test) and cache_pages   :date => "2006-07-04"
.class .methods .instance_variables   :date => "2006-06-14"
select_tag :multiple => true   :date => "2006-06-01"
FileUtils, action_controller rescues   :date => "2006-05-20"
file_field_tag, File.size, File.path, FileUtils.mv   :date => "2006-05-15"
javascript_include_tag, stylesheet_link_tag   :date => "2006-05-02"
submit_to_remote, form_remote_tag, script.aculo.us   :date => "2006-04-30"
periodically_call_remote, simple_format   :date => "2006-04-26"
observe_field - Ajax!   :date => "2006-04-21"
h method, TextHelper, sanitize(), strip_tags()   :date => "2006-04-15"
Rails API :My API [.count(), link_to, text_area :size]   :date => "2006-04-13"
Rails - HTML Select Tag   :date => "2006-04-05"
Pruning Old Sessions   :date => "2006-03-21"
If Elsif Else, and Searching Too!   :date => "2006-03-17"
SHA1 - A quick update   :date => "2006-03-15"
Initialized! good, Authorized? Great! part 2   :date => "2006-03-11"
Initialized! good, Authorized? Great!   :date => "2006-03-08"
Forms and Routing in RoR   :date => "2006-03-06"
My First RoR Post !   :date => "2006-03-05"