2017-03-28 2 views
0

まだ読まれていない私のnotificationsのカウントのためにajaxを作成したいと思います。ajaxとレールを使ってエントリを数える方法

これまでのところ、私はこのすべてがajaxの部分ではなく動作しています。更新されたカウントを表示するには、ページをリロードする必要があります。ページの再読み込みをせずに自動的に更新されるようにしたいと思います。私はAJAXを使用してページをリロードせずに、このカウントを作るために何をする必要がありますどのような

_navbar.html.erb

<% if current_user.notifications.where(read: false).count > 0 %> 
 
     <div style="width:15px;height:15px;border:solid #00ccff 1px;background-color:#00ccff;border-radius:4px;float:right;margin-top:20px;margin-left:-15px;"> 
 
     <div style="padding:2px;margin-top:-6px;"> 
 
      <h style="color:white;"> <%= current_user.notifications.where(read: false).count %> </h> 
 
     </div> 
 
      
 
     </div> 
 
     <% end %>

_navbar.js.erb私はちょうど私がちょうど提出された新しい更新されたものとreplaceに数えてそこに入れていたかわからない。

答えて

0

現在のユーザーの通知を返すだけのエンドポイントを作成します。

class NotificationsController 
    def index 
    @notifications = current_user.notifications 
    render json: @notifications 
    end 
end 

次に、JSファイルに関数を記述する必要があります。

$.ajax(
     {url: '/notifications'} 
    ).done(function (result) { 
     // set the notification to the dom element from here 
     window.setTimeout(this.loadSyncStatus, 5000); 
    }) 
:あなたは jQueryがすでに用意している場合

そして、その後で、このライン上で何かに呼び出されるメソッドを作ります

関連する問題