2

I have this route

namespace :api, defaults: {format: 'json'} do
   namespace :v1 do
      post .....
      get .....
   end
end

So instead of typing localhost:3000/api/v1/func is there a way to make the v1 defaulted (by routes) which then all I have to type is localhost:3000/api/func?

This way if I come out with api v2, all I have to do is change the route in one place.

Thanks!

Nick Colgan
  • 5,488
  • 25
  • 36
Scott Deutsch
  • 637
  • 1
  • 8
  • 24

1 Answers1

1

If you're eventually going to release a new API, then in all likelyhood the routes will probably be different, and the code that calls the routes will probably end up being different too.

I would prefer to take the approach of passing the API version with the request, and then responding accordingly. Depending on the structure of your API, could could just need an API version with the first request, and then the rest would use that, via being saved in a session, etc. This is how long-standing APIs such as Paypal work.

Saying that, if you want to do it, this question provides a way: API Versioning for Rails Routes

Community
  • 1
  • 1
iHiD
  • 2,450
  • 20
  • 32