Search code examples
ruby-on-railsrubyruby-on-rails-5rails-api

Where should my page be located in a Rails API project?


I'm building a Rails API only app, for a game I have had made with pure html/js. For better structure where should be the page located within the large Rails project (will add users and etc). Public? App? Should I create a folder at root level?


Solution

  • You can do it in many ways, If you wanna only provide an APIs.

    Rails only API: Rails API only

    Since only api may be interested in JWT authentication: JWT Sample

    Routes - 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: - sample!

    #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