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 is created in the searches table and then the listings are displayed (dynamically, on the same page the form is submitted from) via the show action for this record.
The results have pagination links provided by kaminari like so:
<%= paginate matches,
:params => {:controller => 'searches',
# I have to specify the id because my searches are stored in the database
:action => 'show', :id => search.id},
:remote => true %>
Note that the pagination links are dynamically included to the page. So, when I do new search and get new listings, the server re-renders the pagination links.
Here is my show action in the searches controller
def show
@search = Search.includes(:rate).find(params[:id])
@matches = @search.matches.order(sort_column + " " + sort_direction).page(params[:page])
respond_to do |format|
format.html
format.xml { render :xml => @matches }
format.js
end
end
For some reason I can’t figure out, all of the parameters I use in the search forms (and there’s a lot of them) are being attached to the kaminari pagination urls giving me hrefs like this:
<a href="/searches/145?massive parameter list omitted" data-remote="true" rel="next">2</a>
The omitted parameter list is so long that it’s too large to be a valid GET request and I get a 414 error code.
As you can see from the searches -> show action I have above, it’s unnecessary for the pagination links to have all this info appended. All they need is the route, id and page number.
How do I prevent this from happening?
By the way, I’ve tried setting :method => :post in the kaminari options. Doesn’t seem to help. I’m using kaminari v 0.12.4 (latest) and Rails 3.1.rc4.
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.erband_last_page.html.erb,_prev_page.html.erband_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 defaultThen edit the partials as follows: