link_to for url with embedded parameters

时间:2015-03-16 23:16:41

标签: ruby-on-rails

我仍然对轨道很新,所以不确定这里的正确条款,但是......

我想看看你的url在结构中嵌入了参数时如何使用link_to帮助器。

如:

http://example.com/appointments/2015/3/15

然后显示某一天的约会。当您知道日期但需要将其作为URL的一部分传递时,如何使用链接?

类似于link_to'Text',appointmentments_path(2015/3/15)或其他什么内容?

对不起,我不确定这里的条款,否则我可能会用谷歌找到它更好

奖励:你怎么称呼这类参数?

3 个答案:

答案 0 :(得分:1)

假设您的路线dynamic segments如下:

get '/appointments/:year/:month/:date', to: 'appointments#index', as: :appointments

您只需将年,月和日参数作为参数传递给路径助手:

link_to appointments_path(year: 2015, month: 3, day: 15)

答案 1 :(得分:0)

您需要自定义路线

get 'appointments/:year/:month/:day', to: 'appointments#show'

...

appointments_path(year: '2015', month: '3', day: '15')

答案 2 :(得分:-2)

在这里查看link_to的rails文档:http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

具体来说,您可以将以下示例用于profile对象:

link_to "Profile", profile_path(@profile)

给你:

<a href="/profiles/1">Profile</a>

相关问题