views:

132

answers:

2

In my website toolbar's I would like to display the number of unread messages in a red circle,

So I think the best way is to create a method in ApplicationController named update_notification :

  def update_notification
     @notification = 42 
     # 42 is just for test
  end

and in application.html.erb, I display :

<%= render :partial => 'messages/notification' %>

_notification.html.erb :

<div id="notification">
   <%= @notification %>
</div>

the problem is where and when can I call update_notification method (in ApplicationController ?) and do you think it's the best way to do it like that ?

Thanks

A: 

I think the best way is to use periodically_call_remote:

<%= periodically_call_remote(:url => { :action => 'update_notification' }) %>

And your action should use rjs to update the element on the page.

Create an rjs file (e.g. update_notification.rjs) with the following content:

page.replace_html "notification", :partial => "messages/notification"

and in your controller render it:

render :action => "update_notification.rjs"
neutrino
Thanks :)I think it's the best way
akam
how can I call the rjs containing "page.replace_html" from update_notification method ?
akam
see the edited answer
neutrino
A: 

before_filter is what you are looking for. Add this to your ApplicationController:

before_filter :update_notification

and your method will be called before every action.

bsboris
Thanks, it could work great
akam