routes.rb中:
# Allows a route to be queried by date
# eg: /any_route/dated/2016-01-01/2016-01-04
concern :datable do
member do
get 'dated/:start_date/(:end_date)', as: 'dated', constraints: {start_date: /\d{4}-\d{2}-\d{2}/, end_date: /\d{4}-\d{2}-\d{2}/}
end
end
# I want to make the receipts "datable"
resources :receipts, only: :index, concerns: :datable, action: :index
看起来很简单,然而,路线正在生成:
# The route I GET
dated_receipt GET /receipts/:id/dated/:start_date(/:end_date)(.:format) receipts#index {:start_date=>/\d{4}-\d{2}-\d{2}/, :end_date=>/\d{4}-\d{2}-\d{2}/}
注意路线中的:id
?那应该不存在,因为我不是在查询特定的收据,只是索引。
我想要生成以下路线(目前尚未发生):
# The route I WANT
dated_receipt GET /receipts/dated/:start_date(/:end_date)(.:format) receipts#index {:start_date=>/\d{4}-\d{2}-\d{2}/, :end_date=>/\d{4}-\d{2}-\d{2}/}
知道为什么:id
被添加到路线中了吗?
答案 0 :(得分:1)
您需要的是collection
上的操作,而不是member
:
collection do
get 'dated/:start_date/(:end_date)', as: 'dated', constraints: {start_date: /\d{4}-\d{2}-\d{2}/, end_date: /\d{4}-\d{2}-\d{2}/}
end
您可以详细了解这些路由方面in docs。