构建API服务器5

2016-06-17  本文已影响23人  yaya_pangdun

Version 2 API

修改config/routes.rb

Rails.application.routes.draw do
 namespace :api do
   namespace :v1 do
     resources :users, only: [:index, :create, :show, :update, :destroy]
   # resources :microposts, only: [:index, :create, :show, :update, :destroy]
   resources :sessions, only: [:create]
   scope path: '/user/:user_id' do
     resources :microposts, only: [:index]
   end
 end
+ namespace :v2 do
+   resources :users, only: [:index, :create, :show, :update, :destroy]
+   resources :sessions, only: [:create]
+   scope path: '/user/:user_id' do
+     resources :microposts, only: [:index]
+   end
+ end
 end
end

生成API::V2::UsersController

$ rails g controller api/v2/users --no-assets

修改app/controllers/api/v2/users_controller.rb

class Api::V2::UsersController < Api::V1::UsersController
 def show
   @user = User.find(params[:id])
 end
end

修改app/views/api/v2/users/show.json.jbuilder

上一篇下一篇

猜你喜欢

热点阅读