Ruby-on-rails 将查询字符串参数添加到 link_to

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2695538/
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 22:35:11  来源:igfitidea点击:

Add querystring parameters to link_to

ruby-on-railsquery-string

提问by craig

I'm having difficultly adding querystring parameters to link_to UrlHelper. I have an Index view, for example, that has UI elements for sorting, filtering, and pagination (via will_paginate). The will_paginate plugin manages the intra-page persistence of querystring parameters correctly.

我很难将查询字符串参数添加到 link_to UrlHelper。例如,我有一个索引视图,它具有用于排序、过滤和分页的 UI 元素(通过 will_paginate)。will_paginate 插件正确管理查询字符串参数的页内持久性。

Is there an automatic mechanism to add the querystring parameters to a give named route, or do I need to do so manually? A great deal of research on this seemingly simple construct has left me clueless.

是否有自动机制将查询字符串参数添加到给定的命名路由,还是我需要手动添加?对这个看似简单的结构的大量研究让我一无所知。

Edit

编辑

Some of the challenges:

一些挑战:

  1. If I have two querystring parameters, bucket & sorting, how do set a specific value to one of these in a link_to, while preserving the current value of the other? For example:

    <%= link_to "0", profiles_path(:bucket => '0', :sorting=>?? ) %>
    
  2. If I have multiple querystring parameters, bucket & sorting & page_size, and I want to set the value to one of these, is there a way to 'automatically' include the names and values of the remaining parameters? For example:

    <%= link_to "0", profiles_path(:bucket => '0', [include sorting and page_size name/values here] ) %>
    
  3. The will_paginate plugin manages its page variable and other querystring variables automatically. There doesn't seem to be an automatic UI element for managing page size. While I've seen code to create a select list of page sizes, I would rather have A elements for this (like SO). Part of this challenge is related to #2, part is related to hiding/showing this UI element based on the existence/non-existence of records. Said another way, I only want to include page-size links if there are records to page. Moreover, I prefer to automatically include the other QS variables (i.e. page, bucket, sorting), rather than having to include them by name in the link_to.

  1. 如果我有两个查询字符串参数,bucket 和排序,如何在 link_to 中将特定值设置为其中一个,同时保留另一个的当前值?例如:

    <%= link_to "0", profiles_path(:bucket => '0', :sorting=>?? ) %>
    
  2. 如果我有多个查询字符串参数、bucket & sorting & page_size,并且我想将值设置为其中之一,有没有办法“自动”包含其余参数的名称和值?例如:

    <%= link_to "0", profiles_path(:bucket => '0', [include sorting and page_size name/values here] ) %>
    
  3. will_paginate 插件自动管理其页面变量和其他查询字符串变量。似乎没有用于管理页面大小的自动 UI 元素。虽然我已经看到创建页面大小选择列表的代码,但我更愿意为此使用 A 元素(如 SO)。此挑战的一部分与 #2 相关,一部分与基于记录的存在/不存在隐藏/显示此 UI 元素有关。换句话说,如果有页面记录,我只想包含页面大小的链接。此外,我更喜欢自动包含其他 QS 变量(即页面、存储桶、排序),而不是在 link_to 中按名称包含它们。

回答by Fred

The API docs on link_toshow some examples of adding querystrings to both named and oldstyle routes. Is this what you want?

link_to 上API 文档显示了一些向命名路由和旧式路由添加查询字符串的示例。这是你想要的吗?

link_tocan also produce links with anchors or query strings:

link_to还可以生成带有锚点或查询字符串的链接:

link_to "Comment wall", profile_path(@profile, :anchor => "wall")
#=> <a href="/profiles/1#wall">Comment wall</a>

link_to "Ruby on Rails search", :controller => "searches", :query => "ruby on rails"
#=> <a href="/searches?query=ruby+on+rails">Ruby on Rails search</a>

link_to "Nonsense search", searches_path(:foo => "bar", :baz => "quux")
#=> <a href="/searches?foo=bar&amp;baz=quux">Nonsense search</a>

回答by u445908

If you want the quick and dirty way and don't worry about XSS attack, use params.mergeto keep previous parameters. e.g.

如果你想要快速和肮脏的方式并且不担心 XSS 攻击,请使用params.merge保留以前的参数。例如

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

see: https://stackoverflow.com/a/4174493/445908

见:https: //stackoverflow.com/a/4174493/445908

Otherwise , check this answer: params.merge and cross site scripting

否则,请检查此答案:params.merge and cross site scripting

回答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 zakelfassi

In case you want to pass in a block, say, for a glyphicon button, as in the following:

如果你想传入一个块,比如一个字形按钮,如下所示:

<%= link_to my_url, class: "stuff" do %>
  <i class="glyphicon glyphicon-inbox></i> Nice glyph-button
<% end %>

Then passing querystrings params could be accomplished through:

然后可以通过以下方式传递查询字符串参数:

<%= link_to url_for(params.merge(my_params: "value")), class: "stuff" do %>
  <i class="glyphicon glyphicon-inbox></i> Nice glyph-button
<% end %>