vendredi 8 août 2014

différentes versions pour les voies d'action contrôleur même 3 - Stack Overflow


I have a action something like:


def show
@p = Post.find(params[:id])
respond_to do |format|
format.html
format.js
end
end

I get a url something:


http://localhost:3000/post/1
http://localhost:3000/post/2
http://localhost:3000/post/3
.
.
.

I want have a different version for this same action something like:


http://localhost:3000/v1/post/1
http://localhost:3000/v1/post/2
http://localhost:3000/v1/post/3
.
.
.

How can I do it?


I have take a look to this resources:


https://github.com/bploetz/versionist


http://railscasts.com/episodes/350-rest-api-versioning?view=asciicast




I guess that the straight forward way would be to use namespacing, e.g. V1::PostsController, V2::PostsController, etc.


See http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing


However, if you're just producing an API, then you should better use https://github.com/intridea/grape , as it has the versioning support baked in.




In your routes file you could add something like


map '/v1/post/:id' => 'yourcontroller#show'


That would be in addition to your existing resources :post or whatever.



I have a action something like:


def show
@p = Post.find(params[:id])
respond_to do |format|
format.html
format.js
end
end

I get a url something:


http://localhost:3000/post/1
http://localhost:3000/post/2
http://localhost:3000/post/3
.
.
.

I want have a different version for this same action something like:


http://localhost:3000/v1/post/1
http://localhost:3000/v1/post/2
http://localhost:3000/v1/post/3
.
.
.

How can I do it?


I have take a look to this resources:


https://github.com/bploetz/versionist


http://railscasts.com/episodes/350-rest-api-versioning?view=asciicast



I guess that the straight forward way would be to use namespacing, e.g. V1::PostsController, V2::PostsController, etc.


See http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing


However, if you're just producing an API, then you should better use https://github.com/intridea/grape , as it has the versioning support baked in.



In your routes file you could add something like


map '/v1/post/:id' => 'yourcontroller#show'


That would be in addition to your existing resources :post or whatever.


0 commentaires:

Enregistrer un commentaire