Search code examples
ruby-on-railsroutessti

Routing for multiple profile views


I no have idea how to implement display of the multiple user's profile. I use STI inheritance to for few types of person.

What I want?

I want to create the simplest routing for each type of person, and possibility to display and edit profile for each type of person. Now I have this: enter image description here

I thought about profile view(backend_people_profile) only for people model, and update_profile for each type. Is it correct? Now I have too many repetitive paths.

routes.rb

namespace :backend do
      resources :managers, except: [:new, :create] do
        get '/profile', to: 'people#show_profile'
      end

      resources :clients, except: [:new, :create] do
        get '/profile', to: 'people#show_profile'
      end

      resources :receptionists, except: [:new, :create] do
        get '/profile', to: 'people#show_profile'
    end

      resources :trainers, except: [:new, :create] do
        get '/profile', to: 'people#show_profile'
      end

      resources :lifeguards, except: [:new, :create] do
        get '/profile', to: 'people#show_profile'
      end
  end

Solution

  • namespace :backend do
      resources :people
    
      [:clients, :receptionists, :trainers, :lifeguards].each |type| do
         get type, to: "people#index"
      end
    end
    

    I would start with the simplest possible setup. In this case you would only have the full CRUD routes for the base people type. This avoids cluttering your API with a ton of routes that perform the exact same thing.

    For each subtype you simply have an index action which is somewhat like:

    GET /people?type=trainer
    

    You may want to consider if you really need separate routes for profiles - unless you need two significantly different representations you can get by with the conventional CRUD routes:

    GET|POST         /people
    GET|DELETE|PATCH /people/:id
    GET              /people/:id/new
    GET              /people/:id/edit
    

    The other case would be an app where users are CRUD:ed by admins where you need a separate interface for the regular user signup. In that case you might to do it like so:

    namespace :backend do
      resources :people
      [:clients, :receptionists, :trainers, :lifeguards].each |type| do
         get type, to: "people#index"
      end
    end
    
    # public facing route
    resources :registrations, only: [:new, :create, :show, :edit, :update]