简体   繁体   中英

Rails 5 Devise multi-user landing pages / custom profiles

I'm not sure if I'm searching for the answer using the wrong terms, but I can't find a good solution to what I'm trying to do, it can't be that unique of a scenario, but I'm a bit of a noob so I apologize if I missed something somewhere.

Rails 5 project using Devise to handle 3 users for a 2-sided marketplace (very close to taskrabbit, ect) : admin, clients, and hosts. Clients build requests, hosts bid on the requests.

When a client logs in I'd like to redirect them to a landing page that has all their current/past requests (and as a consequence the bids, etc). The same for hosts with their bids.

When an admin logs in I want to build an admin panel that lets the admin view all users/hosts.

I'm utterly confused as to how to organize the controllers/views so that clients/hosts can have their own landing page without messing up the index view for when admins log in and would (I think?) use the same clients/index view to get a list of all the users.

Any help would be greatly appreciated. Thanks!

You could redirect from within the Index controller if the logged-in user is an admin, somewhat like:

if current_user.admin?
    redirect_to :admin_view
end

If the three user roles mentioned have totally different properties and meta-data, then create three different devise models and different "home-page" actions for each one of them. Redirect to their respective homepage by overriding the after_sign_in_path_for devise action. As an example, you may create a new folder named "admins" in the controllers folder and create a new file called "registrations_controller.rb"

class Admins::RegistrationsController < Devise::RegistrationsController

  protected     

  def after_sign_in_path_for(resource)
    custom_home_page_path
  end 
end

Repeat the above step for Clients and Hosts as well.

Update 'devise_for' lines in "routes.rb" as follows:

devise_for :admins, controllers: { registrations: 'admins/registrations'}
devise_for :clients, controllers: { registrations: 'clients/registrations'}
devise_for :hosts, controllers: { registrations: 'hosts/registrations'}

However, if you have one universal devise model (say User) and under that you have a column specifying the role, then perform the changes as given below.

In controllers/users/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController

  protected     

  def after_sign_in_path_for(resource)
    if current_user.admin?
      admin_home_page_path
    elsif current_user.client?
      client_home_page_path
    else
      hosts_home_page_path
    end
  end 
end

and in routes.rb

devise_for :users, controllers: { registrations: 'admins/registrations'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM