Unwanted form parameters being appended to pagination links

后端 未结 4 1093
抹茶落季
抹茶落季 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:19

    If someone still has problems with pagination links, there is a fix here: Kaminari: Exclude basic form params from pagination links

    Although, it does not work form me, as it was described in the commit description, there are still unwanted params in the link (:authenticity_token, :commit, :utf8, :_method), but you can exclude them by setting them to nil

    For example:

    paginate @books, params: {authenticity_token: nil, commit: nil, utf8: nil, action: nil}
    

    Result:

    <a href="/books?page=2">2</a>
    

    OR

    Controller:

    def index
      # here search staff, messing our params hash
      @books = Books.all
      @pagination_params = normalize_pagination_params
    end
    
    private
    def normalize_pagination_params
     params.inject({}) do |params_hash, p|
      unless p[0]=="controller"
        params_hash[p[0]] = nil
      end
      params_hash
     end
    end
    

    View:

    paginate @books, params: @pagination_params
    
    0 讨论(0)
  • 2021-02-08 05:19

    Old Post. Better solution.

    If you are using kaminari for pagination for nested resources with ajax updates, you will find Kaminari attempts to build the url based on the current path, regardless of the params you specify, resulting in a routing error.

    The cleanest solution is to use a wildcard route.

    If your paginating comments for a post and have a comments controller with a create and show action, then:

    In your routes:

    match '*path/comments(/:page)' => 'comments#show'
    

    And the pagination widget:

    <%= paginate @comments, params: { controller: :comments }, remote: true %>
    
    0 讨论(0)
  • 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
    %>
    
    <span class="page<%= ' current' if page.current? %>">
      <%= link_to_unless page.current?, page, url, opts = {:remote => remote, :rel => page.next? ? 'next' : page.prev? ? 'prev' : nil} %>
    </span>
    
    # _first_page.html.erb
    <span class="first">
      <% url = url.split('?')[0] + '?page=1' %>
      <%= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, :remote => remote %>
    </span>
    
    # _prev_page.html.erb
    <span class="prev">
      <% 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 %>
    </span>
    
    # _next_page.html.erb
    <span class="next">
      <% 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 %>
    </span>
    
    # _last_page.html.erb
    <span class="last">
      <% url = url.split('?')[0] + '?page=' + num_pages.to_s %>
      <%= link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, {:remote => remote} %>
    </span>
    
    0 讨论(0)
  • 2021-02-08 05:30

    You could try cleaning params by keeping only the things you need and deleting everything else before displaying the search results.

    To use post I think you missed a second step, where you have to override the kaminari link partials : https://github.com/amatsuda/kaminari/wiki/Kaminari-recipes

    0 讨论(0)
提交回复
热议问题