Rails:带参数的 URL/路径 [英] Rails: URL/path with parameters

查看:38
本文介绍了Rails:带参数的 URL/路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个网址

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

从这条路线

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

如何将参数传递给 get_class_swimming_students_path ?

How do I pass parameters to get_class_swimming_students_path ?

推荐答案

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

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

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

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

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

Consider also the following case where foo and bar are static parameters positioned between dynamic parameters:

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

在最后一个示例中,参数将按照传递它们的顺序显示为 URL 参数,但不一定在相同的序列中.

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

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

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')

这篇关于Rails:带参数的 URL/路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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