I am trying to refresh the contents of a div using AJAX/Unobtrusive JavaScript/JQuery.
This is easy to do if I make a "refresh" link and give it a data-remote attribute.
However, I would like to refresh every 2 seconds using the setInterval function rather than using a link.
This code should demonstrate what I'm trying to do. It does not work when put in application.js because the ERB is not evaluated, but hopefully it will explain my goal.
setInterval(function(){
$("#scuttlebutt").html("<%= escape_javascript(render 'scuttlebutt') %>");
},2000);
What would be a proper way to do this in Rails 3??
(I am working with Rails 3.2.5)
I ended up putting code in application.js that would:
Behold...
assets/javascripts/application.js
views/tickets/show.js.erb
Check out this question.
It states that .js files are not parsed like .erb files. They are passed as-is.
Ok, I see what you are trying to do. I have done this before, and I found a simple way of doing it.
You want to still have the button that calls your remote method in a controller that executes some javascript back at the browser to refresh that DIV. I know, I know, you don't want to have it, but here me out. I have one on my page, but I hid it way off to the side so that no one can ever find it, link this:
Then, I added this to that same file, which I presume is a .html.erb file.
This will press the button every 2 seconds, calling your remote function as if some is clicking that button every two seconds. It isn't the cleanest way of doing this, I know, but it works great in a pinch until you can get something more robust working for you. Though you could technically use this.
Hope that helps.