Unwanted Form Parameters Being Appended to Pagination Links

Unwanted form parameters being appended to pagination links

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>

Correct way to handle pagination with form submission?

You have your form configured to submit as a GET request, so you don't really need to force a re-submission through Javascript, you can achieve the same result by setting the next and prev links to URLs that include all the parameters in your form with the page modified to the correct next and previous page numbers.

This is really easy to do with url_for(). Any argument you add that do not match route components will be added to the query string, so you can do something like this:

<a href="{{ url_for('searchresults', categories=form.categories.data, page=form.page.data+1) }}"> Next </a>

One thing to keep in mind is CSRF. If you have it enabled in your form, then your next/prev URLs will also need to have a valid token. Or you can disable CSRF, which for a search form might be okay.

Ajax updating pagination in rails

The problem is that I'm using a POST request, in which parameters such as authenticity key are being passed by the kaminari gem. this is similar to this question:

Unwanted form parameters being appended to pagination links

Kaminari in Apotomo widget, links messed up

This is an issue with Kaminari, I've added a fix in my code that strips the unwanted data from the url that is added by Kaminari in views rendered from an apotomo event.

In application_helper.rb:

  def strip_apotomo_data_from_kaminari_url(url)
url.gsub!('/render_event_response', '')
if url =~ /\?/
param_list = url.split('?')[1].split('&')
param_list.reject!{ |p| p.start_with?('source', 'type') }

url = url.split('?')[0] # strips all params
url += '?'+ param_list.join('&')
end
url
end

Then I called this helper to update the url from the 5 Kaminari pagination link partials: _page.html.erb, _first_page.html.erb and _last_page.html.erb, _prev_page.html.erb and _next_page.html.erb. (see the answer to Unwanted form parameters being appended to pagination links).

Thanks to apotomo's Nick Sutterer for pointing me in the right direction https://groups.google.com/forum/?fromgroups=#!topic/cells-and-apotomo/vekawcXAHN0

The Kaminari bug is: https://github.com/amatsuda/kaminari/issues/131

Showing pagination links based on total number of records

I go with Ojash answer. you can use kaminari and specify the window.

<%= paginate @users, :outer_window => 3 %>

Why is my custom paginator not loading results as it should?

You should append your parameter to each pagination link.
To do so you can use {{$datas->appends(['paginate_number' => request('paginate_number')])->links()}}

I will suggest to change method="POST" to method="GET" in the form.

Laravel Pagination links not including other GET parameters

EDIT: Connor's comment with Mehdi's answer are required to make this work. Thanks to both for their clarifications.

->appends() can accept an array as a parameter, you could pass Input::except('page'), that should do the trick.

Example:

return view('manage/users', [
'users' => $users->appends(Input::except('page'))
]);

Laravel pagination add get parameters

You can append the existing parameters to the links like this:

AppartmentRentDay::where(...)->paginate($paginate)->appends(request()->except('page'));


Related Topics



Leave a reply



Submit