路由添加:id带有问题的索引资源

时间:2016-01-05 18:44:59

标签: ruby-on-rails ruby-on-rails-4 routing routes

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被添加到路线中了吗?

1 个答案:

答案 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