2017-10-21 107 views
0

我正在构建一个Rails API only应用程序,用于使用纯html/js制作的游戏。为了更好的结构,应该是大型Rails项目中的页面(将添加用户等)。上市?应用程序?我应该在根级创建一个文件夹吗?我的页面应该放在Rails API项目中的哪个位置?

+1

经验法则是在'/ api'中存储api控制器和视图。所以,你可以存储在'应用程序/视图/ API的意见/ [name_of_controller]/[name_of_view] *' –

+0

如果它是一个仅API的Rails应用程序,然后我把JavaScript客户端代码到一个分离的存储库,并会部署客户端到另一个地方。 – spickermann

回答

1

你可以做到这一点在很多方面,如果你想只提供的API。

Rails的唯一的API:Rails API only

因为只有API可能感兴趣的JWT认证:JWT Sample

路线 - 样!

namespace :api do 
    namespace :v1, defaults: { format: :json } do 
     resources :orders, only: [:index, :show,:create] do 
      member do 
       post 'cancel' 
       post 'status' 
       post 'confirmation' 
      end 
     end 

     # Users 
     resources :users, only: [] do 
      collection do 
       post 'confirm' 
       post 'sign_in' 
       post 'sign_up' 
       post 'email_update' 
       put 'update' 
      end 
     end 
    end 
end 

#output 
... 
GET /api/v1/orders(.:format) api/v1/orders#index {:format=>:json} 
POST /api/v1/orders(.:format)     api/v1/orders#create {:format=>:json} 
GET /api/v1/orders/:id(.:format)    api/v1/orders#show {:format=>:json} 
POST /api/v1/users/confirm(.:format)   api/v1/users#confirm {:format=>:json} 
POST /api/v1/users/sign_in(.:format)   api/v1/users#sign_in {:format=>:json}  

Controlers: - 样!

#application_controller.rb 
class ApplicationController < ActionController::API 
end 

#api/v1/app_controller.rb 
module Api 
    class V1::AppController < ApplicationController 
     ...  
    end 
end 

#api/v1/users_controller.rb 
module Api 
    class V1::UsersController < V1::AppController 
     ... 
    end 
end 
相关问题