Rails Routes for Versioned Web Services
I'm new to Rails. Can anyone tell me if the f开发者_运维知识库ollowing is possible, and if you, what would the correct "route" entry be?
I have a controller ItemController with one action (index). When I set it up by default, it's accessible at http://my.app:3000/item, but I want this controller (i.e. this controller only, not the other controllers) to be accessible via http://my.app:3000/api/v1/.
Thanks x
Vanessa
Set it up under a namespace:
namespace :api do
namespace :v1 do
resources :items
end
end
Then you would need to move this controller into app/controllers/api/v1/items_controller.rb
and call the class defined in it Api::V1::ItemsController
.
Chances are that you're going to h ave some custom logic you want for API actions, so you may want to put that logic in another controller other than ApplicationController
. This means this new controller would inherit from Api::V1::BaseController
rather than ApplicationController
.
On a side note, I cover this kind of thing in the "Designing an API" chapter of my book, Rails 3 in Action.
精彩评论