并非所有成员路由都在查询字符串中获得 id

Not all member routes get an id in the querystring

某些具有特定名称的成员路由未在查询字符串中添加预期的 :id。 这些路线:

resources :tests do
  member do
    get :foo
  end
end

resources :contacts do
  member do
    get :foo
  end
end


resource :tasks do
  member do
    get :foo
  end
end

resource :fruits do
  member do
    get :foo
  end
end

resource :cars do
  member do
    get :foo
  end
end

resources :appointments do
  member do
    get :foo
  end
end

产生这个:

           foo_test GET          /tests/:id/foo(.:format)                   tests#foo
        foo_contact GET          /contacts/:id/foo(.:format)                contacts#foo
          foo_tasks GET          /tasks/foo(.:format)                       tasks#foo
         foo_fruits GET          /fruits/foo(.:format)                      fruits#foo
           foo_cars GET          /cars/foo(.:format)                        cars#foo
    foo_appointment GET          /appointments/:id/foo(.:format)            appointments#foo

任务、水果和汽车没有id。这是为什么?

您的问题在于 resource(单数)与 resources(复数)。您会注意到,每当您的路线(测试、联系、约会)中有一个 :id 时,它都会使用资源声明,否则使用资源声明。

通过使用 resource(单数),您是在隐含地表示您只有一种这种类型的资源,因此不需要 ID 和 ID,这就是它不在此处的原因。

您可以在此处找到更多信息:Difference between resource and resources methods