Rails:在 link_to 中保留 GET 查询字符串参数

发布于 2024-09-24 01:21:40 字数 512 浏览 8 评论 0原文

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

/search?q=test&page=2

现在假设我需要显示一组将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

有没有一种简单的方法可以仅使用 link_to helper 来完成此操作,或者我需要以某种方式解析并重现以前请求中的查询字符串?

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:

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

/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?

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

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

发布评论

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

评论(8

雾里花 2024-10-01 01:21:40
link_to 'Link', request.query_parameters.merge({:per_page => 20})
link_to 'Link', request.query_parameters.merge({:per_page => 20})
堇色安年 2024-10-01 01:21:40
link_to 'Link', params.merge({:per_page => 20})
link_to 'Link', params.merge({:per_page => 20})
不…忘初心 2024-10-01 01:21:40

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

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

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

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

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=bar instead of ?foo=bar.

哥,最终变帅啦 2024-10-01 01:21:40

如果您想保留现有参数并且不让自己遭受 XSS 攻击,请务必清理参数哈希,仅保留您的应用程序可以发送的参数

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

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

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

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

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:

# 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) %>
扮仙女 2024-10-01 01:21:40

您只需将 params 哈希值的元素抛出到 link_to 即可。喜欢

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

You can just throw elements of the params hash at link_to. Like

link_to "some_other_link", "/search", :page => params[:page]
最偏执的依靠 2024-10-01 01:21:40

如果您正在处理的链接不是通过 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

This works if the links you are processing aren't given to you by 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
梦年海沫深 2024-10-01 01:21:40

又怎样呢

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

What about

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

?

自在安然 2024-10-01 01:21:40

我知道有点晚了..

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

这会自动删除所有空白和不需要的参数,并添加“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

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

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

或者在您的情况下:

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

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.

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 '$80 to $110', :price => 80..110

Or in your case:

search_to '30 per page', :page => params[:page], :per_page => 30
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文