Reputation: 5197
In background, I want it to reload and shows the number how many unread messages are there.
I want that without refreshing page. I mean using ajax.
If I had this in menu, how can I refresh only this section every 30 secs?
<li><%= link_to sanitize('<i class="icon-envelope"></i> ') + "received messages" + sanitize(' <span class="badge badge-info">'+current_user.mailbox.inbox(:read => false).count(:id, :distinct => true).to_s+'</span>'), messages_received_path %></li>
messages_controller.rb
def received
if params[:search]
@messages = current_user.mailbox.inbox.search_messages(@search).page(params[:page]).per(10)
else
@messages = current_user.mailbox.inbox.page(params[:page]).per(10)
end
add_crumb 'Messages Received', messages_received_path
@box = 'inbox'
render :index
end
Upvotes: 0
Views: 176
Reputation: 151
Something like this should work:
controller
def messages_received
@messages = Messages.messages_received
respond_to |format|
format.json { render json: @messages }
end
end
js
setInterval(function(){
$.ajax({
type: 'GET',
url: '/messages_received',
data: {},
success: function(JSONRESPONSE){
alert(JSONRESPONSE) // @messages variable
};
})
},30000) // 30 segs
Upvotes: 1
Reputation: 901
You should have an action which returns you something like a number of unread messages and poll it using the JS function setInterval()
Upvotes: 1