Ruby-on-rails Rails:带参数的 URL/路径

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

Rails: URL/path with parameters

ruby-on-railsrubyruby-on-rails-3

提问by wwli

I would like to produce a URL as

我想生成一个 URL 作为

/swimming/students/get_times/2013-01-01/2013-02-02

from this route

从这条路线

get_class_swimming_students GET /swimming/students/get_times/:start_date/:end_date(.:format) swimming/students#get_times

How do I pass parameters to get_class_swimming_students_path?

如何将参数传递给get_class_swimming_students_path

回答by zeantsoi

get_class_swimming_students_path('2013-01-01', '2013-02-02')

In Rails, URL parameters are mapped to the router in the precise order in which they are passed. Consider the following:

在 Rails 中,URL 参数按照它们传递的精确顺序映射到路由器。考虑以下:

# rake routes
my_route GET    /my_route/:first_param/:second_param/:third_param(.:format)

# my_view.html.erb
<%= link_to('My Route', my_route_path('first_param', 'second_param', 'third_param') %>
#=> <a href="/my_route/first_param/second_param/third_param">My Route</a>

Consider also the following case where fooand barare static parameters positioned betweendynamic parameters:

还要考虑以下情况,其中foobar位于动态参数之间的静态参数:

# rake routes
my_route GET    /my_route/:first_param/foo/:second_param/bar/:third_param(.:format)

# my_view.html.erb
<%= link_to('My Route', my_route_path('first_param', 'second_param', 'third_param') %>
#=> <a href="/my_route/first_param/foo/second_param/bar/third_param">My Route</a>

In the final example, arguments will appear as URL parameters in the orderin which they were passed, but not necessarily in the same sequence.

在最后一个示例中,参数将按照传递的顺序作为 URL 参数出现,但不一定以相同的顺序出现

EDIT:

编辑:

The following syntax is equivalent to the first snippet. The primary difference is that it accepts arguments as named parameters, rather than in the order they're passed:

以下语法等效于第一个代码段。主要区别在于它接受参数作为命名参数,而不是按照它们传递的顺序:

get_class_swimming_students_path(:start_date => '2013-01-01', :end_date => '2013-02-02')

回答by Jyothu

For any path you can pass params like

对于任何路径,您都可以传递参数,例如

get_class_swimming_students_path(:params1 => value1, :params2 => value2)

And In controller you can simply access those passed params as usual

在控制器中,您可以像往常一样简单地访问那些传递的参数

回答by Sachin R

Try to use that

尝试使用它

get_class_swimming_students_path(:start_date => "2013-01-01", :end_date => "2013-02-02")

回答by Jerome

in rails 4, type less with:

在 rails 4 中,输入 less:

get_class_swimming_students_path(params1: value1, params2: "value2")

where value1 is a method and value2 is a string

其中 value1 是一个方法,value2 是一个字符串

回答by Zhenya

You can also combine named parameters and not named parameters like this:

您还可以像这样组合命名参数和未命名参数:

get_class_swimming_students_path('2013-01-01', '2013-02-02', gender: 'M')