gpt4 book ai didi

ruby-on-rails - rails : URL/path with parameters

转载 作者:数据小太阳 更新时间:2023-10-29 06:23:15 24 4
gpt4 key购买 nike

我想生成一个 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

最佳答案

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

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

还请考虑以下情况,其中 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>

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

编辑:

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

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

关于ruby-on-rails - rails : URL/path with parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16954289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com