rails will_paginate with different partials

1k Views Asked by At

I have some new actions for the notification model and things started getting messy so I refactored from <% = render @other_notifications %> with notifcations/_notification.html.erb to the following structure.

My problem is the following. The page renders everything well, but the pagination doesn't work properly. So IF I have the structure below and doesn't delete _notification.html.erb then the page will be loaded with the new action partials and the pagination objects will be loaded with _notification.html.erb. IF I delete _notification.html.erb then the page still loads with the new partials, but pagination doesnt't work. How should I change the pagination to make this work?

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

There are 1 best solutions below

0
Sean Magyar On

I solved it like this. So will paginate will look for _notification partial, which will always be found with the following code, and the _notification partial will call the rest of the partials.

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 %>