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

2023-12-24

我有一个页面,用于通过使用提供的表单提交数据来搜索列表。表单参数通过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。


如果有人仍然对分页链接有问题,可以在这里修复:Kaminari:从分页链接中排除基本表单参数 https://github.com/amatsuda/kaminari/commit/2fd7d36b72af73d2506f8e2ab68704d804f70fc5

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

例如:

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

Result:

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

OR

控制器:

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐