Rails 3 工具栏渲染

4
在我的网站工具栏中,我想要显示未读消息数量的红色圆圈。所以我认为最好的方法是在ApplicationController中创建一个名为update_notification的方法:
  def update_notification
     @notification = 42 
     # 42 is just for test
  end

在 application.html.erb 文件中,我显示如下内容:
<%= render :partial => 'messages/notification' %>

_notification.html.erb :

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

问题在于我应该在哪里和何时调用update_notification方法(在ApplicationController中吗?)你认为这样做是最好的方式吗?
谢谢。
2个回答

3

我认为最好的方法是使用periodically_call_remote

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

您的操作应该使用rjs来更新页面上的元素。

创建一个名为(例如update_notification.rjs)的rjs文件,其内容如下:

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

在你的控制器中进行渲染:

render :action => "update_notification.rjs"

我认为这是最好的方式。 - akam
我该如何从update_notification方法中调用包含“page.replace_html”的rjs文件? - akam

0

before_filter 是你要找的东西。将此添加到你的 ApplicationController 中:

before_filter :update_notification

你的方法将在每个操作之前被调用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接