How can I make a POST call instead of GET call with will_paginate?

微笑、不失礼 提交于 2019-12-23 05:32:11

问题


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:

  1. 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

  2. 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.

  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!