rails will_paginate与不同的部分

时间:2016-05-21 10:40:18

标签: ruby-on-rails pagination will-paginate unobtrusive-javascript

我对通知模型有一些新操作,事情开始变得混乱,所以我从<% = 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 %>

1 个答案:

答案 0 :(得分:0)

我这样解决了。因此,paginate将查找_notification partial,这将始终使用以下代码找到,_notification partial将调用其余部分。

other_notifications.html.erb

<%= render @other_notifications %>
<%= will_paginate @other_notifications %>

_notification.html.erb

<% if lookup_context.template_exists?(notification.action, "notifications/actions", true) %>
  <%= render partial: "notifications/actions/#{notification.action}", locals: { notification: notification } %>
<% end %>