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

是否有一种自动机制来添加查询字符串参数给指定路由,还是我需要手动这样做?关于这个看似简单的结构的大量研究让我毫无头绪。

Edit

以下是挑战:

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=>?? ) %> 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] ) %> 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.


当前回答

如果你想保留现有的参数而不暴露自己的XSS攻击,一定要清理params散列,只留下你的应用程序可以发送的参数:

# 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) %>

其他回答

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

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>

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

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

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

否则,检查这个答案:params。合并和跨站点脚本编制

你可以像官方rails指南中提到的那样使用data-属性:

  <%= link_to 'Link', link_path, data: { params: "per_page=20" } %>

控制器中的参数将是:

{“per_page”= >“20”等……

这个例子可以通过params[:per_page]来访问

如果你想保留现有的参数而不暴露自己的XSS攻击,一定要清理params散列,只留下你的应用程序可以发送的参数:

# 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) %>

如果你想传入一个块,比如说,一个象形图标按钮,如下所示:

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

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

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