2
如何爲現有的rails應用程序構建版本API? 我已經建立了與用戶的關係,船舶和公司如何爲現有的rails應用程序構建版本API?
Company has_many users
User belongs_to company
一個簡單的應用程序如何建立一個版本API爲這個應用程序
如何爲現有的rails應用程序構建版本API? 我已經建立了與用戶的關係,船舶和公司如何爲現有的rails應用程序構建版本API?
Company has_many users
User belongs_to company
一個簡單的應用程序如何建立一個版本API爲這個應用程序
原料藥
這裏的目錄結構,ApiController是ApplicationController的子類,並充當所有其他API控制器的父類。
app/controllers/
.
|-- api
| `-- v1
| |-- api_controller.rb
| |-- users_controller.rb
|-- companies_controller.rb
|-- application_controller.rb
這裏是控制器的樣子:
module Api::V1
class ApiController < ApplicationController
#API stuff here
end
end
module Api::V1
class UsersController < ApiController
# POST /v1/users
def create
your code goes here
end
end
end
API路線
routes.rb
namespace :api, defaults: {format: :json} do
namespace :v1 do
resources :users
end
end
用戶的index action API的網址,只需例如
http://localhost:3000/api/v1/users.json
thnks LHH,可我知道這將是用於獲取JSON數據的網址是什麼? – febil
我已經更新了我的答案 – LHH