How to show a Timer for lock expiry when editing a record

I'm newbie to ruby on rails. In my application, i was trying to show the remaining time the user is allowed to edit an instance record( like milestones, cheklists etc.). I was locking it from multiple users to edit it at the sametime. When the user clicks the edit link_to i need to display the timer in the edit.rhtml

I have the javascript function for timer updation. when called it updates the text_field with the time.

function update_timer() {         var page_loaded_ms = new Date().getTime();         var timeout_ms = 100*1000;         var time_left = Math.round((timeout_ms - new Date().getTime() + page_loaded_ms) / 1000);         var minutes = Math.floor(time_left/60);         var seconds = time_left - (minutes*60);         $('time_left_value').value = 'Time left: '+ minutes + ' min' + zeroPad(seconds, 2)+ ' sec';         setTimeout("update_timer()", 1000); }

function zeroPad(number, width) {         var s = number.toString();         while (s.length < width)                 s = "0" + s;         return s; }

could anyone suggest me a solution for this.

Vijay wrote:

I'm newbie to ruby on rails. In my application, i was trying to show the remaining time the user is allowed to edit an instance record( like milestones, cheklists etc.). I was locking it from multiple users to edit it at the sametime. When the user clicks the edit link_to i need to display the timer in the edit.rhtml

I thought for a second about attempting to answer this, but quickly realized. DO NOT DO THIS!

Pessimistic locking on databases is very, very bad form. Do not attempt to prevent concurrent editing. You're just asking for trouble. Users of modern applications will not accept pessimistic style record locking.

Instead, develop a recovery solution based on an optimistic locking scheme. ActiveRecord has some built-in support for optimistic locking, which will get you started. If the optimistic locking mechanism detects concurrent edits then present that to the user with enough information to recover.

P.S. Using *.rhtml extensions is so one-dot-ex. If you're using a version of Rails 2.0 or higher, switch your views to use *.html.erb.