【发布时间】:2016-09-18 14:41:45
【问题描述】:
我对通知模型有一些新的操作,事情开始变得混乱,所以我从 <% = render @other_notifications %> 和 notifcations/_notification.html.erb 重构为以下结构。
我的问题如下。该页面可以很好地呈现所有内容,但分页无法正常工作。所以如果我有下面的结构并且不删除_notification.html.erb,那么页面将加载新的动作部分,分页对象将加载_notification.html.erb。如果我删除_notification.html.erb,那么页面仍然会加载新的部分,但分页不起作用。我应该如何更改分页以使其正常工作?
notifications_controller.rb
def other_notifications
@other_notifications = current_user.notifications.not_chat.order(created_at: :desc).includes(:sender_profile).
paginate(page: params[:page], per_page: Notification.pagination_per_page)
current_user.reset_new_other_notifications
respond_to do |format|
format.html
format.js
end
end
other_notifications.html.erb
<div class = "other-notifications-index">
<% @other_notifications.each do |notification| %>
<% if lookup_context.template_exists?(notification.action, "notifications/actions", true) %>
<%= render partial: "notifications/actions/#{notification.action}", locals: { notification: notification } %>
<% end %>
<% end %>
</div>
<div id="infinite-othernotification-scrolling">
<%= will_paginate @other_notifications %>
</div>
other_notifications.js.erb
$('.other-notifications-index').append('<%= j render @other_notifications %>');
<% if @other_notifications.next_page %>
$('.pagination').replaceWith('<%= j will_paginate @other_notifications %>');
<% else %>
$(window).off('scroll');
$('.pagination').remove();
$('.no-more').delay(1000).fadeIn(1500);
<% end %>
【问题讨论】:
标签: ruby-on-rails pagination will-paginate unobtrusive-javascript