Home > Back-end >  Is there a Rails pattern for giving different kinds of users different home pages?
Is there a Rails pattern for giving different kinds of users different home pages?

Time:05-07

My initial thought is to just have a home_page attribute on the User model and then do a redirect_to after authentication. Is it as simple as that, or is there a better way?

As background, here are some of the use cases:

One user can be an admin, and can impersonate other users on the site for support purposes. Our admins do this so that they can "see" what the customer is seeing. When logging in, an admin will be directed to a page that shows current usage statistics as well as any errors that are occurring across the application.

Another type of user is a collaborator from a partner marketplace. Let's say that company is called Amazing. A manager from Amazing can log in and see all of the users that are selling products on the Amazing marketplace, yet he doesn't have the same access as an Admin. I want to redirect this user to a page that shows all of the sellers on the Amazing marketplace.

A standard customer will be directed to a dashboard page that will help them manage advertisements on a marketplace.

CodePudding user response:

You can differentiate home page for user if using Devise. On an old project I did something like this, code is not so clean, but you can get an idea:

routes.rb

Rails.application.routes.draw do
  mount Ckeditor::Engine => '/ckeditor'

  devise_for :admins,
             :frontend_operators,
             :backend_operators,
             class_name: 'User',
             path: '',
             controllers: {sessions: 'frontend/auth/sessions',
                           confirmations: 'frontend/auth/confirmations',
                           unlocks: 'frontend/auth/unlocks',
                           passwords: 'frontend/auth/passwords',
                           registrations: 'frontend/auth/registrations'},
             path_names: {sign_in: 'login',
                          sign_out: 'logout',
                          password: 'secret',
                          confirmation: 'verification',
                          sign_up: 'register',
                          edit: 'profile/edit'}
...

  authenticated :backend_operator do
    get :backend_operator_root, action: :index, controller: "backend/home"
  end
  authenticated :frontend_operator do
    get :frontend_operator_root, action: :index, controller: "frontend/home"
  end
  authenticated :admin do
    get :admin_root, action: :index, controller: "backend/home"
  end
end

application_controller.rb

class ApplicationController < ActionController::Base

  devise_group :user, contains: [:backend_operator, :frontend_operator, :admin]

  [:backend_operator, :frontend_operator, :admin].each do |model|

    define_method("decorated_current_#{model}") { send("current_#{model}").decorate }
    helper_method "decorated_current_#{model}".to_sym
  end

  before_action :set_root, unless: :root_present?

  protected

  def set_root
    Rails.application.config.root_url = root_url
  end
end
  • Related