不需要的表单参数被附加到分页链接

发布于 2024-11-15 09:42:26 字数 1260 浏览 6 评论 0原文

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

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

<%= 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 %>

请注意,分页链接是动态包含到页面中的。因此,当我进行新的搜索并获取新的列表时,服务器会重新呈现分页链接。

这是我在搜索控制器中的显示操作

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 分页 url 上,给我这样的 href :

<a href="/searches/145?massive parameter list omitted" data-remote="true" rel="next">2</a>

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

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

我该如何防止这种情况发生?

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

隔岸观火 2024-11-22 09:42:26

如果有人仍然遇到分页链接问题,这里有一个解决方案:Kaminari:从分页链接中排除基本表单参数

虽然,它对我不起作用,正如提交描述中所描述的那样,但仍然有链接中不需要的参数(:authenticity_token、:commit、:utf8、:_method),但您可以通过将它们设置为 nil 来排除它们,

例如:

paginate @books, params: {authenticity_token: nil, commit: nil, utf8: nil, action: nil}

结果:

<a href="/books?page=2">2</a>

控制器:

def index
  # here search staff, messing our params hash
  @books = Books.all
  @pagination_params = normalize_pagination_params
end

private
def normalize_pagination_params
 params.inject({}) do |params_hash, p|
  unless p[0]=="controller"
    params_hash[p[0]] = nil
  end
  params_hash
 end
end

视图:

paginate @books, params: @pagination_params

If someone still has problems with pagination links, there is a fix here: Kaminari: Exclude basic form params from pagination links

Although, it does not work form me, as it was described in the commit description, there are still unwanted params in the link (:authenticity_token, :commit, :utf8, :_method), but you can exclude them by setting them to nil

For example:

paginate @books, params: {authenticity_token: nil, commit: nil, utf8: nil, action: nil}

Result:

<a href="/books?page=2">2</a>

OR

Controller:

def index
  # here search staff, messing our params hash
  @books = Books.all
  @pagination_params = normalize_pagination_params
end

private
def normalize_pagination_params
 params.inject({}) do |params_hash, p|
  unless p[0]=="controller"
    params_hash[p[0]] = nil
  end
  params_hash
 end
end

View:

paginate @books, params: @pagination_params
吾家有女初长成 2024-11-22 09:42:26

一般想法

您可以通过编辑分页部分来手动从 url 中删除参数,然后添加页面参数来解决此问题。我知道这是一个黑客行为,但如果分页损坏(就像对我来说),这似乎是解决此问题的最快方法。

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

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

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

具体说明

如果您还没有这样做,请通过运行 rails g kaminari:views default 在您的应用中生成分页部分,

然后按如下方式编辑部分:

#_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>

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.erb and _last_page.html.erb, _prev_page.html.erb and _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 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>
z祗昰~ 2024-11-22 09:42:26

您可以尝试通过仅保留您需要的内容并在显示搜索结果之前删除其他所有内容来清理参数。

要使用帖子,我认为您错过了第二步,您必须覆盖 kaminari 链接部分: https://github.com/amatsuda/kaminari/wiki/Kaminari-recipes

You could try cleaning params by keeping only the things you need and deleting everything else before displaying the search results.

To use post I think you missed a second step, where you have to override the kaminari link partials : https://github.com/amatsuda/kaminari/wiki/Kaminari-recipes

久而酒知 2024-11-22 09:42:26

旧邮报。更好的解决方案。

如果您使用 kaminari 对具有 ajax 更新的嵌套资源进行分页,您会发现 Kaminari 会尝试基于当前路径构建 url,而不管您指定的参数如何,从而导致路由错误。

最简洁的解决方案是使用通配符路由。

如果您对帖子的分页评论并且有一个带有创建和显示操作的评论控制器,那么:

在您的路线中:

match '*path/comments(/:page)' => 'comments#show'

以及分页小部件:

<%= paginate @comments, params: { controller: :comments }, remote: true %>

Old Post. Better solution.

If you are using kaminari for pagination for nested resources with ajax updates, you will find Kaminari attempts to build the url based on the current path, regardless of the params you specify, resulting in a routing error.

The cleanest solution is to use a wildcard route.

If your paginating comments for a post and have a comments controller with a create and show action, then:

In your routes:

match '*path/comments(/:page)' => 'comments#show'

And the pagination widget:

<%= paginate @comments, params: { controller: :comments }, remote: true %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文