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