1
votes

I am currently working on a Rails 5.1 application that has two different User roles: "Admin" and "Customer". (I am storing the roles in a column named "role" #string on the User's table).

I am looking for the most efficient way to render different views based on the User Role. e.g.

  • If a user with the user.role = "Customer" logs in, I would like to render customer#index

  • If a user with the user.role = "Admin" logs in, I would like to render admin#index

Right now the route root is "admin#index". Is there a way to change the root route based on the role of the current user?

Any tips for the most efficient way of rendering different views based on user role will be appreciated! Thank you so much!

1

1 Answers

0
votes

You can specify the after_sign_in_path_for route for a resource. So

application_controller.rb

....

def after_sign_in_path_for(resource)
  if resource.role == "Customer"
    customers_path (or whatever the name of the path is in your routes)
  elsif resource.role == "Admin"
    admins_path (same here, whatever the name of the path is in your routes)
  else
    super
  end
end

....

There are a bunch of methods like this that you might find useful digging through, for this specific one https://www.rubydoc.info/github/plataformatec/devise/Devise/Controllers/Helpers:after_sign_in_path_for, or for all options https://www.rubydoc.info/github/plataformatec/devise/Devise/Controllers/Helpers.