开发者

Sending a Javascript request in rails

Im trying to make a system that informs the user of any new private messages, the index method of the messages controller creates to instance variables of @messages and @newmessages, both are used in index.html.erb and i want to make it so that index.js.erb makes the flash message box on my site appear.

Ive got jquery to run a function every few seconds, the function that gets run is

function checkagain(){
 //Start a Timer, dont want it bombarding the server with constant requests
 $.timer(10000,function(){
  $.getScript("/messages.js");
  //RECURSE, see you again in 10 seconds
  checkagain();
 });
}

$.getScript doesnt seem to make it run the code on index.js.erb which is simply:

alert("RESPONDING");

I cant seem to get it to trigger the code on that page.

I have found that /messages.js that doesn't return any javascript, just the same as index.html.erb

the index action of the controller is:

def index
    @messages = Message.find(:all, :conditions => "receiver_id = '#{current_user.id}' AND `read` = '1'")
    @newmessages = Message.find(:all, :conditions => "receiver_id = '#{current_us开发者_如何学JAVAer.id}' AND `read` = '0'")
end


Try appending a random query string to force your browser to re-download the js file:

$.getScript("/messages.js?random=" + Math.random());


Could you post some code from the index action of your messages controller? This doesn't appear to be a jquery issue if messages.js isn't returning the javascript you expect.

Do you have a messages/index.js.erb view that is supposed to return that alert call, or do you have a messages.js file in your public directory somewhere?


You could have a look at Juggernaut which will allow you to push to the client browser using a Flash 8 resource.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜