不需要的表单参数将附加到分页链接 [英] Unwanted form parameters being appended to pagination links

查看:71
本文介绍了不需要的表单参数将附加到分页链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,该页面用于通过使用提供的表单提交数据来搜索列表.表单参数通过ajax(后请求)提交,在搜索表中创建一个新记录,然后通过show操作为此记录显示列表(动态地,在表单提交的同一页面上).

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.

结果具有kaminari提供的分页链接,如下所示:

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

由于某种原因,我无法弄清楚,我在搜索表单中使用的所有参数(其中有很多)都附加在kaminari分页网址上,从而为我提供了如下的hrefs:

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>

所省略的参数列表太长,以至于太大而无法成为有效的GET请求,并且我收到了414错误代码.

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.

从搜索中可以看到->显示上面的操作,分页链接不必附加所有这些信息.他们所需要的只是路线,ID和页码.

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?

顺便说一句,我尝试在kaminari选项中设置:method => :post.似乎没有帮助.我正在使用kaminari v 0.12.4(最新版本)和Rails 3.1.rc4.

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.

推荐答案

总体思路

您可以通过以下方法解决此问题:编辑分页部分,以手动从url中删除参数,然后重新添加页面参数.我知道这是一种黑客行为,但如果分页被破坏(对我而言),这似乎是解决此问题的最快方法.

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

我正在从此问题的GitHub错误报告中发布的解决方案中扩展该问题.

您必须编辑5个分页链接部分中的每一个:_page.html.erb(按编号),_first_page.html.erb_last_page.html.erb_prev_page.html.erb_next_page.html.erb.

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.

您可以从局部变量中提供的局部变量中找到所需的页码:current_pagepagenum_pages.

You can find the page number you want from the local variables made available in the partials: current_page, page, num_pages.

如果还没有,请通过运行rails g kaminari:views default

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>

这篇关于不需要的表单参数将附加到分页链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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