将查询字符串参数添加到 link_to [英] Add querystring parameters to link_to

查看:37
本文介绍了将查询字符串参数添加到 link_to的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

编辑

一些挑战:

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

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

  • 如果我有多个查询字符串参数,bucket &排序 &page_size,我想将值设置为其中之一,有没有办法自动"包含其余参数的名称和值?例如:

  • 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] ) %>
    

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

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

    推荐答案

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

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

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

    link_to can also produce links with anchors or query strings:

    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>
    

    这篇关于将查询字符串参数添加到 link_to的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆