如何添加链接标记作为读取通知栏?

时间:2014-11-14 23:17:21

标签: ruby-on-rails ruby-on-rails-4

在我的应用中有一个通知系统,它看起来像这样: 应用程序/视图/ static_pages / notification.html.erb

<div class="notification">
    <h4>notification</h4>
<% @activities.each do |activity| %>
    <ul>
        <li>
    <%= render "activities/#{activity.trackable_type.underscore}/#{activity.action}", activity: activity %>
        </li>
    </ul>
<% end %>
<%= link_to "mark all as read"%>
</div>

并在应用程序控制器中有代码

def activity
    if signed_in?
      @activities = current_user.activities.includes(:author, :trackable).where(read:false).order(created_at: :desc)  
    end
  end

db activity有“读取”布尔列,当用户点击链接<%= link_to "mark all as read"%>通知视图但我不知道如何做到这一点时,我需要将此列更新为true?请帮忙!感谢的

1 个答案:

答案 0 :(得分:1)

  1. 创建新的控制器方法read_all,内容为:

    def read_all
      current_user.activities.where(read: false).update_all(read: true)
    end
    
  2. 设置路线。如果您在ActivitiesController中设置了read_all方法,请执行:

    resources :activities do
      post :read_all, on: :collection
    end
    
  3. 建立链接:

    <%= button_to "Read all", read_all_activities_path, method: :post %>