我正在使用 Rails 4.0,并且正在尝试使以下路线起作用:
class Api::V1::MyController
def get # maps to http GET
end
def post # maps to http POST
end
... Same for PATCH PUT DELETE
end
而且我无法弄清楚正确的routes.rb。
我尝试了几种变体:
namespace :api do
namespace :v1 do
match ':controller(/:action(/:id))', via: [:get, :put, :post, :patch, :delete]
end
end
导致错误:“名称空间块中不允许控制器段”
和
match '/api/v1/:controller(/:action(/:id))', via: [:get, :put, :post, :patch, :delete]
导致错误:“自动加载常量 ApiController 时检测到循环依赖”
这就是我希望 URL 的样子:
http://www.localhost.com/api/v1/my_controller/1234
or eventually:
http://www.localhost.com/api/v1/photos/1234
http://www.localhost.com/api/v1/users1234
http://www.localhost.com/api/v1/albums/1234
http://www.localhost.com/api/v1/puppies/1234