0

我有一个类似的动作:

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

我得到一个网址:

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

对于相同的操作,我想要一个不同的版本,例如:

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

我该怎么做?

我看了一下这个资源:

https://github.com/bploetz/versionist

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

4

2 回答 2

1

我想直接的方法是使用命名空间,例如 V1::PostsController、V2::PostsController 等。

请参阅http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

但是,如果您只是生成一个 API,那么您最好使用https://github.com/intridea/grape,因为它具有版本控制支持。

于 2012-07-01T14:28:26.120 回答
0

在您的路线文件中,您可以添加类似

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

那将是您现有resources :post或其他内容的补充。

于 2012-07-01T14:28:51.247 回答