2017-10-21 6 views
0

私は純粋なhtml/jsで作ったゲームのために、Rails APIのみのアプリケーションを構築しています。より良い構造のために、大きなRailsプロジェクト内にあるページはどこにあるべきですか(ユーザーなどを追加します)。パブリック?アプリ?ルートレベルでフォルダを作成する必要がありますか?私のページはRails APIプロジェクトのどこにあるべきですか?

+1

経験則は、APIコントローラとビューを '/ api'に格納することです。したがって、ビューを 'app/views/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}  

コントローラー: - サンプル!

#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 
関連する問題