同一控制器动作轨道3的不同版本

时间:2012-07-01 14:20:44

标签: ruby-on-rails ruby-on-rails-3 version

我的行动类似于:

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

2 个答案:

答案 0 :(得分:1)

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

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

但是,如果您只是在制作API,那么最好使用https://github.com/intridea/grape,因为它已经提供了版本控制支持。

答案 1 :(得分:0)

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

的内容

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

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

相关问题