Unwanted form parameters being appended to pagination links

后端 未结 4 1107
抹茶落季
抹茶落季 2021-02-08 04:29

I have a page which is used for searching through listings by submitting data using the supplied forms. The form parameters are submitted via ajax (post request), a new record i

4条回答
  •  渐次进展
    2021-02-08 05:29

    General Idea

    You can fix this by editing the pagination partials to manually strip out the params from the url, then add the page param back. I know this is a hack, but it seems like the quickest way to fix this issue if pagination is broken (as it was for me).

    I'm expanding this from the solution posted in the GitHub bug report for this issue.

    You have to edit each of the 5 pagination link partials: _page.html.erb (by number), _first_page.html.erb and _last_page.html.erb, _prev_page.html.erb and _next_page.html.erb.

    You can find the page number you want from the local variables made available in the partials: current_page, page, num_pages.

    Specific Instructions

    If you haven't already, generate the pagination partials in your app by running rails g kaminari:views default

    Then edit the partials as follows:

    #_page.html.erb
    <%
     unless page.current?
       url = url.split('?')[0] + '?page=' + page.to_s
     end
    %>
    
    
      <%= link_to_unless page.current?, page, url, opts = {:remote => remote, :rel => page.next? ? 'next' : page.prev? ? 'prev' : nil} %>
    
    
    # _first_page.html.erb
    
      <% url = url.split('?')[0] + '?page=1' %>
      <%= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, :remote => remote %>
    
    
    # _prev_page.html.erb
    
      <% url = url.split('?')[0] + '?page=' + (current_page.to_i - 1).to_s %>
      <%= link_to_unless current_page.first?, raw(t 'views.pagination.previous'), url, :rel => 'prev', :remote => remote %>
    
    
    # _next_page.html.erb
    
      <% url = url.split('?')[0] + '?page=' + (current_page.to_i + 1).to_s %>
      <%= link_to_unless current_page.last?, raw(t 'views.pagination.next'), url, :rel => 'next', :remote => remote %>
    
    
    # _last_page.html.erb
    
      <% url = url.split('?')[0] + '?page=' + num_pages.to_s %>
      <%= link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, {:remote => remote} %>
    
    

提交回复
热议问题