notifications - Rails 5.2 无法使用 kaminari 和通知进行分页
问题描述
我的一些gemfiles:
gem 'rails', '~> 5.2'
gem 'notifications', '~> 0.6.0'
gem 'kaminari'
我notifications_controller.rb
是这样的:
# notifications_controller.rb
module Notifications
class NotificationsController < Notifications::ApplicationController
def index
@notifications = notifications.includes(:actor).order("id desc").page(params[:page])
unread_ids = @notifications.reject(&:read?).select(&:id)
Notification.read!(unread_ids)
@notification_groups = @notifications.group_by { |note| note.created_at.to_date }
end
def clean
notifications.delete_all
redirect_to notifications_path
end
private
def notifications
raise "You need reqiure user login for /notifications page." unless current_user
Notification.where(user_id: current_user.id)
end
end
end
我的routes.rb
关于这样的通知:
mount Notifications::Engine, at: "notifications"
我的index.html
关于这样的通知:
# notifications/notifications/index.html.erb
<div class="paginationBox">
<%= paginate @notifications,left:2,right:1,window: 1 %>
</div>
使用上述代码,我可以获得正确的通知,但我无法分页所有通知。我有这样的错误:
NoMethodError - undefined method `total_pages' for #<Notification::ActiveRecord_Relation:0x00007fcfacae6dd8>:
app/views/notifications/notifications/index.html.erb:33:in `_app_views_notifications_notifications_index_html_erb__2430601159943313679_70264929101860'
如果我 将动作更改index
为notifications_controller.rb
@notifications = notifications.includes(:actor).order("id desc").page(params[:page]).per(10)
我得到相同的错误未定义方法total_pages
....
如果我只是将通知index.html.erb
更改为
<div class="paginationBox">
<%= paginate@notifications.page(params[:page]).per(10),left:2,right:1,window: 1 %>
</div>
它不显示任何错误,但kaminari
不能分页所有通知。
那么,我在哪里有错误?请你帮助我好吗 ?非常感谢!
解决方案
推荐阅读
- python - 在 python 中使用 response.json() 时出现属性错误
- algorithm - 具有多个对象的基于网格的寻路
- c - 使用缓冲区删除字符串中的重复项
- python - 当字段可以为无时如何使用 timedelta 进行过滤?
- python - 如何使用 BeautifulSoup 找到类 _XWk?
- javascript - 如何使 URL 参数匹配可选
- typescript - ts-jest 返回“类型实例化过深并且可能无限”,但 tsc 编译正常
- c - 如何使用 C 以编程方式更改设置以使 Linux 终端中字符的宽度和高度相同而没有任何间距?
- python - 在 Lambda 函数中使用 OR - Web Scraping Python
- java - 为什么?状态:Google Play 上的 Wear OS 不接受应用程序