在我的routes.rb中:
resources :posts do
get "test"
end
这会生成/post/:id/...
的常用RESTful路由。但是,我也得到/post/:post_id/test
。
现在我的问题是,有时参数名为:id,有时它是:post_id。我怎样才能使它统一?
谢谢!
答案 0 :(得分:5)
指定:on => :member,否则它充当嵌套资源。
resources :posts do
get 'test', :on => :member
end
答案 1 :(得分:1)
你不应该让它统一。当它是目标资源时为:id
,而当它是某些其他目标资源(即嵌套资源)的父资源时为:post_id
。这是一个Rails惯例。