Ruby-on-rails Rails:在link_to 中保留GET 查询字符串参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3762430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:14:43  来源:igfitidea点击:

Rails: Preserving GET query string parameters in link_to

ruby-on-railsparametersroutinghelpers

提问by Vincent

I have a typical search facility in my app which returns a list of results that can be paginated, sorted, viewed with a different records_per_page value, etc. Each of these options is controlled by parameters in the query string. A simplified example:

我的应用程序中有一个典型的搜索工具,它返回一个结果列表,这些结果列表可以分页、排序、使用不同的 records_per_page 值查看等。这些选项中的每一个都由查询字符串中的参数控制。一个简化的例子:

/search?q=test&page=2

Now say I need to display a set of links that set records_per_page value to 10, 20, 30. Each link must include the existing query parameters, which can be a very long set, plus a new per_page parameter.

现在说我需要显示一组将records_per_page 值设置为10、20、30 的链接。每个链接必须包括现有的查询参数,这可以是一个很长的集合,加上一个新的per_page 参数。

/search?q=test&page=2& ... &per_page=10
/search?q=test&page=2& ... &per_page=20
/search?q=test&page=2& ... &per_page=30

Is there an easy way to do it with just link_to helper or I need to parse and reproduce the query string from previous request somehow?

是否有一种简单的方法可以仅使用 link_to 助手来完成,或者我需要以某种方式解析和重现先前请求中的查询字符串?

回答by Vincent

link_to 'Link', request.query_parameters.merge({:per_page => 20})

回答by Olek

link_to 'Link', params.merge({:per_page => 20})

回答by Clemens

The simplest way to merge the new params with the query parameters and NOT with all parameters (including those obtained through the path) is to merge with request.query_parameters

将新参数与查询参数合并而不与所有参数(包括通过路径获得的参数)合并的最简单方法是与request.query_parameters合并

link_to 'Search', search_path(request.query_parameters.merge({ per_page: 20 }))

Otherwise you end up with query strings duplicating the path parameters, for example ?action=index&controller=products&foo=barinstead of ?foo=bar.

否则,您最终会得到复制路径参数的查询字符串,例如?action=index&controller=products&foo=bar而不是?foo=bar.

回答by Greg Funtusov

If you want to keep existing params and not expose yourself to XSS attacks, be sure to clean the params hash, leaving only the params that your app can be sending:

如果你想保留现有的参数并且不让自己暴露在 XSS 攻击中,请务必清除 params 哈希,只留下你的应用程序可以发送的参数:

# inline
<%= link_to 'Link', params.slice(:sort).merge(per_page: 20) %>

 

 

If you use it in multiple places, clean the params in the controller:

如果在多个地方使用它,请清理控制器中的参数:

# your_controller.rb
@params = params.slice(:sort, :per_page)

# view
<%= link_to 'Link', @params.merge(per_page: 20) %>

回答by Reactormonk

You can just throw elements of the paramshash at link_to. Like

您可以将params散列的元素扔到link_to. 喜欢

link_to "some_other_link", "/search", :page => params[:page]

回答by jordinl

What about

关于什么

<%= link_to 'Whatever', :overwrite_params => { :pear_page => 20 } %>

?

?

回答by Joe Van Dyk

This works if the links you are processing aren't given to you by request.params.

如果 request.params 没有提供您正在处理的链接,则此方法有效。

require 'rack/utils'                                                                                
require 'uri'                                                                                       

def modify_query url, options={}                                                                    
  uri = URI(url)                                                                                    
  query_hash = Rack::Utils.parse_query(uri.query)                                                   
  query_hash.merge!(options)                                                                        
  uri.query = Rack::Utils.build_query(query_hash)                                                   
  uri.to_s                                                                                          
end                                                                                                 

puts modify_query('/search?q=test&page=2&per_page=10', 'per_page' =>  20)                           
puts modify_query('/search?q=test&page=2', 'per_page' => 30)                                        

# Outputs                                                                                           
# /search?q=test&page=2&per_page=20                                                                 
# /search?q=test&page=2&per_page=30

回答by complistic

A bit late i know..

知道的有点晚了。。

If your using this as a way to filter search results have a look at my helper :)

如果您使用它来过滤搜索结果,请查看我的助手 :)

This automagicly removes all blank and unneeded params and add the class "selected" if all of it's new params were already set.

如果所有新参数都已设置,这会自动删除所有空白和不需要的参数,并添加“selected”类。

def search_to s, args={}

  selected = 0
  args.each do |k, v|
    selected = selected + 1 if params[k] == v.to_s || ( params[k].nil? && v.blank? )
  end

  if @search_params_base.nil?
    @search_params_base = request.parameters.clone
    @search_params_base.delete(:action)
    @search_params_base.delete(:controller)
    @search_params_base.delete(:page)
    @search_params_base.delete_if{|k, v| v.nil? || v.blank?}
    @search_params_base.delete(:utf8) if @search_params_base[:keywords].nil?
  end
  search_params = @search_params_base.merge(args)
  search_params.delete_if{|k, v| v.nil? || v.blank?}

  link_to s, search_path + '?' + search_params.to_param, :class => selected == args.length ? 'selected' : nil
end

You can then just use this in your view:

然后你可以在你的视图中使用它:

search_to ' to 0', :price => 80..110

Or in your case:

或者在你的情况下:

search_to '30 per page', :page => params[:page], :per_page => 30