分页是否适用于具有 method="POST" 的表单? [英] Does will pagination work with forms which have method="POST"?

查看:41
本文介绍了分页是否适用于具有 method="POST" 的表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的高级订单从 GET 切换为 POST,因为 URI 请求变得太大,浏览器无法处理.一切正常,除了会分页.它不断将页面添加到 url 本身 http://localhost:3000/orders/advanced_search 就像 http://localhost:3000/orders/advanced_search?page=2失败了,因为这是一个 post 调用,而不是 get 调用.

I switched my advanced order form to POST from GET as the URI request became too large for browsers to handle. Everything works fine, with the exception of will pagaination. It keeps adding the page to the url itself http://localhost:3000/orders/advanced_search like http://localhost:3000/orders/advanced_search?page=2 which fails as this is a post call, and not a get call.

它可以只更新 params[:page] 但不对链接做任何事情的任何方式?

Any way that it can just update the params[:page] but not do anything to the link?

我曾经只调用 <%= will_paginate @orders["order_items"] %> 当它是一个 GET 调用时效果很好

I used to just call <%= will_paginate @orders["order_items"] %> which worked great when it was a GET call where

@orders["order_items"] = @orders["order_items"].paginate(:page => params[:page],:per_page =>限制,:total_entries=>@orders["total"])

我想要的很简单,把它加到params里,但不要加到链接里.

What I want is simple, add it to params, but do not add it to the link.

推荐答案

发布的另一个答案是错误的.will_paginate 不是为处理帖子请求而构建的.

The other answer posted is wrong. will_paginate was NOT built to work with post requests.

您选择使"will_paginate 处理帖子请求包括:

Your choices to 'make' will_paginate work with post request include:

编写一些 javascript 以:

  • preventDefault 点击 will_paginate 生成的链接

$(".pagination li a").click(function(e){e.preventDefault();....

$(".pagination li a").click(function(e){ e.preventDefault(); ....

  • 通过点击的链接找到目标页面.
  • (下面的代码假设所有 will_paginate 链接都有一个查询字符串……他们确实这样做了.但请注意,如果您通过 will_paginate 的 params 选项在控制器中传递更多参数,则此特定代码行将不起作用..首先您不应该这样做,因为我们正在尝试实现发布请求.)

    (below code is assuming all will_paginate links have a query string...which they do. But note that this particular line of code won't work if you are passing in more params in the controller via the params option for will_paginate..which you shouldn't be in the first place because we are trying to achieve a post request.)

    var tp_id = $(this).attr("href").split('?page=')[1]

    var tp_id = $(this).attr("href").split('?page=')[1]

    • 根据单击的 will_paginate 链接生成具有正确名称属性的隐藏输入.
    • $('form').append("< input type='hidden' name='page' value='" + tp_id +"' >")

      $('form').append("< input type='hidden' name='page' value='" + tp_id +"' >")

      • 最后发布到搜索表单的控制器操作.
      • $('form').submit()

        $('form').submit()

        .完成.我希望这能让你走上正确的道路.

        .done. I hope this puts you on the right path.

        这篇关于分页是否适用于具有 method="POST" 的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆