What you want to do is creating a container that will display your text and use javascript to update it's content. To tell the (client-side) javascript to what time it shall count down, you might create a hidden field containing that value, and read it out using jQuery. (Note: Passing the value could also be archieved through AJAX+embedded ruby in the JS-file, but it is much tougher to understand, and creating a hidden field in your html.erb should do your job even a bit faster!)
- Retrieving the hidden field's content (a.k.a. the moment in time that your countdown is leading to):
var end_time = $('hidden-field').text();
#hidden-field {
display:none;
}
<span id='hidden-field'><%= time %></span>
(Note: As rails comes with jQuery, using it saves a lot of headdache. However, usually including the jQuery-library for this would be considered a "cannon to kill a fly". Also, make sure to use the correct CSS-Selecter within the jQuery call.)
You could also simply add this to your html.erb to assign a variable:
<script type='text/javascript'>var end_time = <%= time %> ;</script>
Depending on the format of your time (datetime vs unix timestamp vs...) you then let Javascript do the math. As rails usually saves Daytime-elements in the database, this should work:
var time_left = Math.abs(new Date(end_time) - new Date());
To insert into your page, do this:
$('#result').text(time_left);
#result {
// Some styling here
}
<p id='result'></p>
To make it work automatically from that point, combine the last two to a function like
var time_left;
function getTimeLeft() {
time_left = Math.abs(new Date(end_time) - new Date());
$('#result').text(time_left);
}
This last call will run your function every second
setInterval(getTimeLeft, 1000);
I did not test the code, but it should work or at least give the basic idea how you could do it. If you run into errors, please comment.
Happy coding.
P.S. Jacob's answer is shorter and more on the point. Try it first and come back here if you need to go further.