observe_field in rails

There used to be this method observe_field in rails upto v2.3.8 under ActionView::Helpers::PrototypeHelper. To give more details – here is the snipper from apidock

observe_field(field_id, options = {}) public

Observes the field with the DOM ID specified by field_id and calls a callback when its contents have changed. The default callback is an Ajax call. By default the value of the observed field is sent as a parameter with the Ajax call.

You need to specify url for the ajax call / a javascript function that you want to execute on the change of the field. Additional options give you flexibility to specify how frequently the action is supposed to take place, options to specify more parameters to the ajax call. 
The use case for this is something like selecting from a dropdown, and updating some content on the page based on the selection. 

However the caveat here is, you cannot do something like the jQuery live functionality. If you have more select boxes coming up later after the page has already loaded, your ajax calls won’t happen on selecting something from the new dropdown. 

Secondly this seems to work only for the change event on fields although on dom elements I would like to have reused the same method for multiple other events like ‘click’, ‘mouseover’, and so many more.

In addition, there is an observe_form method that can help out a bit with the 1st problem by observing all fields within the form for changes, however with dynamic elements (not in the original source of the form) coming up it again fails. (Jquery has made life so much easier and natural rather than trying out weird solutions to do stuff like this.)

So whats up with this method now?

This method has been deprecated. In fact the PrototypeHelper has seen a lot of change. Legacy prototype helpers are available as a plugin now. https://github.com/rails/prototype_legacy_helper in favour of making Rails 3 agnostic of javascript frameworks.

Ofcourse, jQuery can handle the above scenarios with much more flexibility and cleaner code. 

So if you are upgrading a legacy project which has usages of observe_field, it would be advisable to go for jQuery and keep it clean, simple and working 🙂



SimplyHelpful was a plugin used in legacy rails apps for generating the right dom id, css classes for records. For more info read http://juixe.com/techknow/index.php/2007/04/03/simply-helpful-rails-plugin/. However, Simplyhelpful is now in rails. So you will no longer need the plugin. And methods like dom_id are available out of the box now. Just get the scoping correct.


To give a specific example, for the dom_id method, usages may have been in helpers or views, and in that case you may get errors where Rails starts looking for dom_id method under ActionView. You will have to fix this. You need the RecordIdentifier module of ActionController.




Getting started on Rails upgrade (Rails 1 to Rails 2)

There are plugins now to upgrade from rails 2 to rails 3 (For details look at https://github.com/rails/rails_upgrade). However for rails 1 to rails 3 its a huge jump. So its most advisable to go from rails 1 to rails 2. And then use the plugin to help with the rest of the upgrade.

So when we started off with this task, we found a great post here http://marklunds.com/articles/one/409. It gives a great starting point to get the ball rolling. 

Few things you need to keep in mind, 

1) Steps 1, 2, 3 mentioned in the post above may not be needed if you are not planning to vendorize rails. 

2) Its best to introduce bundler at a point you deem comfortable since that can help you out with managing gem dependencies very comfortably. You can follow the steps given in http://gembundler.com/rails23.html to make bundler work with rails 2.

3) Also when you are looking for corresponding gems or plugins for rails 2, it would be helpful to investigate more into what the plugin/gem actually does since quite a few gem/plugin behaviours got integrated into rails later or new better plugins got introduced for them.

At the same time, if you can’t find options, just investigate more into the errors you get after migrating to rails 2, and try to solve them. It may be as simple as using ActiveSupport::Inflector instead of Inflector, moving inflections to the right place, etc. 

At every point keep referring apidock, guides.rubyonrails.org, rdoc for reference on which methods are deprecated and what are their corresponding alternatives now.



Rails upgrade

Recently I have been working on upgrading a legacy app from Rails 1.2.6 to 2.3.11. No doubt it has been a difficult task primarily because

  • It demands good knowledge of how things worked in the old Rails, how they do now
  • lack of enough documentation on Rails 1.2.6 currently available on the web.
  • Usage of gems and plugins which are no longer supported anymore.
  • And life can just become worse if you have custom plugins.

Nonetheless, whatever help I received from the web prompted me to start off with this series. As Matz says Ruby community is a good community, I think its important for us to continue the tradition by giving back to the community in the form of sharing anything that can help people out there.

I will keep posting anything interesting or anything that took me a lot of pain to solve in this series, in short my learnings.