如何从API提供的URL设置路由?

时间:2015-05-06 14:51:09

标签: ruby-on-rails

我希望能够显示Twilio录音

网址是

/2010-04-01/Accounts/{AccountSid}/Recordings/{RecordingSid}

日期(2010-04-01)由= @recording.DateCreated引用 {AccountSid} = @recording.AccountSid {RecordingSid} = @recording.Sid

我如何创建路线? 得到......

另外,我如何在列表中显示这些网址:

<% @recordings.each do |recording| %>
 <li>
  <%= link_to recording? %>
 </li>
<% end %>

1 个答案:

答案 0 :(得分:1)

在config / routes.rb中:

date_rx = /^(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$/

get '/:date/Accounts/:account_sid/Recordings/:recording_sid',
  to: 'mycontroller#myaction', as: 'recording',
  constraints: { date: date_rx, account_sid: /\d+/, recording_sid: /\d+/ }

在视图中:

<% @recordings.each do |recording| %>
  <li>
    <%= link_to recording_path(recording.date, recording.AccountSid, 
          recording.RecordingSid) %>
  </li>
<% end %>