【问题标题】:Rails 3 Toolbar renderRails 3 工具栏渲染
【发布时间】:2010-06-09 16:48:31
【问题描述】:

在我的网站工具栏中,我想在红色圆圈中显示未读消息的数量,

所以我认为最好的方法是在 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 中?),你认为这是最好的方法吗?

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 render


    【解决方案1】:

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

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

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

    创建一个包含以下内容的 rjs 文件(例如update_notification.rjs):

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

    然后在你的控制器中渲染它:

    render :action => "update_notification.rjs"
    

    【讨论】:

    • 谢谢 :) 我认为这是最好的方法
    • 如何从 update_notification 方法调用包含“page.replace_html”的rjs?
    【解决方案2】:

    before_filter 是您正在寻找的。将此添加到您的 ApplicationController:

    before_filter :update_notification
    

    你的方法会在每一个动作之前被调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      相关资源
      最近更新 更多