2016-04-12 53 views

回答

3

原料药

这里的目录结构,ApiController是ApplicationController的子类,并充当所有其他API控制器的父类。

app/controllers/ 
    . 
    |-- api 
    | `-- v1 
    |  |-- api_controller.rb 
    |  |-- users_controller.rb 
      |-- companies_controller.rb 
    |-- application_controller.rb 

这里是控制器的样子:

应用程序/控制器/ API/V1/api_controller.rb

module Api::V1 
    class ApiController < ApplicationController 
    #API stuff here 
    end 
end 

应用程序/控制器/ API/V1/users_controller.rb

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 
+0

thnks LHH,可我知道这将是用于获取JSON数据的网址是什么? – febil

+0

我已经更新了我的答案 – LHH

相关问题