これが正しいアプローチかどうかはわかりませんが、タイトルの内容を正確に伝えたいと思います。私はそれに慣れていないので、いくつかのjQueryの概念が欠けています。
私はjQuery.restを使用しています:https ://github.com/lyconic/jquery.rest
このスクリプトは、オンラインユーザーとオフラインユーザーが更新されているかどうかを定期的にチェックします。
<!-- Online checker -->
<script type="text/javascript">
$(function(){
setInterval (checkTrackings, 5000);
var js = '<checkTrackings> ';
function checkTrackings(){
$.read(
'/users/{id}/check',
{ id: <%= @user.id.to_json %> },
function (response) {
$("ul#online").empty();
$("ul#offline").empty();
// Code to add users back to list
$("ul#online").html('<%= escape_javascript(render(@online_users)).html_safe %>')
$("ul#offline").html('<%= escape_javascript(render(@offline_users)).html_safe %>')
console.log(js + 'Check trackings');
}
);
}
});
</script>
ただし、@ online_usersと@offline_usersの代わりに、返されたJSONオブジェクトが必要であり、それらを上記の変数に変換する必要があります。このスクリプトが呼び出すメソッドは次のとおりです。
# Updates contacts
def check
@online_users = Array.new
@offline_users = Array.new
@user = User.find(params[:id])
@followed_users = @user.followed_users
@followed_users.each do |followed_user|
@online_users.push(followed_user) if followed_user.state.online
end
@offline_users = @followed_users - @online_users
respond_to do |format|
format.js { render :json => { :online_users => @online_users, :offline_users => @offline_users } }
end
end
他のヒントもいいでしょう。私はWeb開発にとても慣れていません。助けてくれてありがとう!