我有一个通知页面,我只想显示用户没有看到的通知。我试图通过使每个通知都做到这一点:默认情况下,读取=> false,但当用户点击通知链接('.bell'类)时将其更改为:read => true。问题在于,当用户点击链接时,它被更改为=> true,所以当通知页面加载所有内容时,它已被“读取”并且不显示任何内容。我不确定如何延迟此更改,直到页面加载完毕。延迟从未读更新到读取
在我的控制器我有这样的:
def notifications
@activities = PublicActivity::Activity.order("created_at desc").where(recipient_id: current_user.id)
@notification_count = @activities.where(:read => false).count
end
def read_all_notifications
PublicActivity::Activity.where(recipient_id: current_user.id).update_all(:read => true)
end
在我的路线
然后:
get 'users/:id/allnotifications', to: 'users#allnotifications', as: :allnotifications
post 'users/read_all_notifications', to: 'users#read_all_notifications', as: :read_all_notifications
和users.js.coffee:
$(document).on 'click' , '.bell' , (e)->
$.ajax '/users/read_all_notifications' ,
type: "post"
dataType: "json"
beforeSend: (xhr) ->
xhr.setRequestHeader "X-CSRF-Token", $("meta[name=\"csrf-token\"]").attr("content")
cache: false
编辑
我现在把这个在我通知的底部查看:
<script>
$(document)()->
$.ajax '/users/read_all_notifications' ,
type: "post"
dataType: "json"
beforeSend: (xhr) ->
xhr.setRequestHeader "X-CSRF-Token", $("meta[name=\"csrf-token\"]").attr("content")
cache: false
</script>
根本没有想法,这是有效的! – user2759575 2014-09-26 19:49:28