Rails Routing - :resource_id vs :id



在我的路线中。rb:

resources :posts do
    get "test"
end

这产生了具有/post/:id/...的常见RESTful路由。然而,我也得到了/post/:post_id/test

现在我的问题是,有时参数的名称是:id,有时它是:post_id。我怎样才能使它统一?

谢谢!

指定:on=>:成员,否则它将充当嵌套资源。

resources :posts do
    get 'test', :on => :member
end

您不应该使其统一。当它是目标资源时,它是:id,当它是某些其他目标资源(即嵌套资源)的父资源时,是:post_id。这是一个Rails约定。

最新更新