【问题标题】:rails will_paginate with different partialsrails will_paginate 使用不同的部分
【发布时间】: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


    【解决方案1】:

    我是这样解决的。所以 paginate 会寻找_notificationpartial,它总是会在下面的代码中找到,_notificationpartial 会调用其余的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 %>
    

    【讨论】:

      猜你喜欢
      • 2012-06-16
      • 1970-01-01
      • 2013-12-09
      • 2016-04-20
      • 2012-01-01
      • 1970-01-01
      • 2013-07-25
      • 2014-02-20
      • 2010-10-03
      相关资源
      最近更新 更多