Ruby-on-rails 使用 url_for 查询参数?

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

Query parameters with url_for?

ruby-on-railsruby-on-rails-3query-parametersurl-for

提问by randomguy

url_for([:edit, @post])

is working and generating /comments/123/edit. Now I need to add a query parameter, so that instead of

正在工作和生成/comments/123/edit. 现在我需要添加一个查询参数,而不是

/comments/123/edit

it is

这是

/comments/123/edit?qp=asdf

I tried url_for([:edit, @post], :qp => "asdf")but no go.

我试过了,url_for([:edit, @post], :qp => "asdf")但不行。

采纳答案by Simone Carletti

Use named routes.

使用命名路由。

edit_post_path(@post, :qp => "asdf")

回答by epochwolf

You can use polymorphic_path

您可以使用 polymorphic_path

polymorphic_path([:edit, @post], :qp => 'asdf')

回答by Michael Smart

The answerfrom Simone Carletti indeed works, but there are times when one wants to construct the URL using objects as described in the Rails routing guide, and not rely on the _pathhelpers.

Simone Carletti的答案确实有效,但有时人们想要使用 Rails 路由指南中描述的对象构建 URL,而不是依赖_path帮助程序。

The answers from both Benand Swardsare attempting to describe exactly how to do this, but for me the syntax used results in an error (using Rails 4.2.2, which has the same behaviour as 4.2.4, which is the current stable release as of this answer).

BenSwards的答案都试图准确地描述如何做到这一点,但对我来说,使用的语法会导致错误(使用 Rails 4.2.2,它与 4.2.4 具有相同的行为,这是当前的稳定版本从这个答案开始)。

The correct syntax for creating a URL/path from objects while also passing parameters should be, as opposed to a nested array, rather a flat array containing the URL components, plus a hash as the final element:

从对象创建 URL/路径同时传递参数的正确语法应该是,而不是嵌套数组,而不是包含 URL 组件的平面数组,加上哈希作为最终元素:

url_for([:edit, @post, my_parameter: "parameter_value"])

url_for([:edit, @post, my_parameter: "parameter_value"])

Here the first two elements are parsed as the components for the URL, and the hash is considered as parameter(s) for the URL.

这里前两个元素被解析为 URL 的组件,哈希被视为 URL 的参数。

This also works with link_to:

这也适用于link_to

link_to( "Link Text", [:edit, @post, my_parameter: "parameter_value"])

link_to( "Link Text", [:edit, @post, my_parameter: "parameter_value"])

When I call url_foras suggested by Ben & Swards:

当我url_for按照 Ben & Swards 的建议打电话时:

url_for([[:edit, @post], my_parameter: "parameter_value"])

url_for([[:edit, @post], my_parameter: "parameter_value"])

I get the following error:

我收到以下错误:

ActionView::Template::Error (undefined method 'to_model' for #<Array:0x007f5151f87240>)

ActionView::Template::Error (undefined method 'to_model' for #<Array:0x007f5151f87240>)

The trace shows that this is being called from polymorphic_routes.rbin ActionDispatch::Routing, via url_forfrom routing_url_for.rb(ActionView::RoutingUrlFor):

跟踪显示这是从polymorphic_routes.rbin调用的ActionDispatch::Routing,通过url_forfrom routing_url_for.rb( ActionView::RoutingUrlFor):

gems/actionpack-4.2.2/lib/action_dispatch/routing/polymorphic_routes.rb:297:in `handle_list'
gems/actionpack-4.2.2/lib/action_dispatch/routing/polymorphic_routes.rb:206:in `polymorphic_method'
gems/actionpack-4.2.2/lib/action_dispatch/routing/polymorphic_routes.rb:134:in `polymorphic_path'
gems/actionview-4.2.2/lib/action_view/routing_url_for.rb:99:in `url_for'

The problem being, that it's expecting an array of URL components (eg. symbols, model objects, etc), notan array containing another array.

问题是,它需要一个 URL 组件数组(例如符号、模型对象等),而不是包含另一个数组的数组。

Looking at the appropriate codefrom routing_url_for.rb, we can see that when it receives an array which has a hash as the final element, it will then extractthe hash and treat as parameters, leaving then just the array with the URL components.

纵观适当的代码routing_url_for.rb,我们可以看到,当它接收具有哈希值作为最终的元素的数组,它就会提取哈希和治疗作为参数,然后留下刚与URL组件阵列。

Which is why the flat array with a hash as the last element works, and the nested array does not.

这就是为什么以散列作为最后一个元素的平面数组有效,而嵌套数组无效的原因。