问题
How can I make a post call instead of get call when i am paginating through pages using rails gem will_paginate?
In controller I have
@bills = Bill.where(some_condition) # returns collection
@bills = @bills.paginate(:page => params[:page], :per_page => 5)
In view have used a table structure and at the end, have used <%will_paginate%>
, hence page 1 is showing up, but when I click on "next" or "page 2" then GET req. goes, I want to change it to POST call, is it possible?
回答1:
I agree with antiqe's comment it's not right to paginate through POST request.
But if you have made up your mind to use POST read below:
By modifying the will_paginate
Source enchante's comment
use will_paginate doesn't support post by default, if you insist to use post, you need to customize LinkRender for will_paginate, see reference: github.com/mislav/will_paginate/wiki/Link-renderer
By custom POST requests
From the source:
the only correct way to do pagination in HTML would be to render a form inside which each pagination link is a submit button with the attribute name="page" set to page number as value. This form should also preserve existing search parameters, therefore it should have a hidden field for each of the current parameters. Clicking on the submit button will resubmit the advanced search with the page parameter changed.
By custom ajax request
Or You can write a javascript to prevent the default get request on pagination links and send ajac post request instead. see more here
The reason will_paginate
don't support POST is that nobody uses it.
回答2:
That's possible. But not suggest to do so.
@bills = @bills.paginate(:page => params[:page], :per_page => 5)
Here the params[:page] controls which page should be shown, include :page parameter in the post, that should work.
Updated:
will_paginate doesn't support post by default, if you insist to use post, you need to customize LinkRender for will_paginate, see reference: github.com/mislav/will_paginate/wiki/Link-renderer
来源:https://stackoverflow.com/questions/25300684/how-can-i-make-a-post-call-instead-of-get-call-with-will-paginate