简体   繁体   English

设计中的多个用户

[英]Multiple Users in Devise

After 2 full days I have not been able to find a solution. 整整2天后,我无法找到解决方案。

Here's my issue. 这是我的问题。

I created a rails app and added Devise. 我创建了一个Rails应用并添加了Devise。 It all works great except for I have 3 devise users: Users , Vendors and Admin . 除了我有3个devise用户之外,其他所有功能都很棒: UsersVendorsAdmin

My problem is that I am trying to create separate login pages for each user. 我的问题是我试图为每个用户创建单独的登录页面。 However when I generate devise views and work on the login page it only pulls the HTML from the views/devise/sessions/new page. 但是,当我生成设计视图并在登录页面上工作时,它仅从视图/设计/会话/新页面中提取HTML。 So whether you go /admin/sign_up , /vendor/sign_up or /user/sign_up you get the exact same devise page from views/devise/sessions/new . 因此,无论您进入/admin/sign_up/vendor/sign_up还是/user/sign_up您都可以从views/devise/sessions/new /user/sign_up views/devise/sessions/new获得完全相同的/user/sign_up页面。

How can you have 3 different devise/sessions/new views ? 您如何拥有3种不同的devise/sessions/new views I want the User login and sign up to look different from the Vendors and Admin logins. 我希望User登录和注册看起来与VendorsAdmin登录不同。 I have 3 devise controllers: Users_controller, Vendors_controller, and Admins_controller 我有3个设备控制器:Users_controller,Vendors_controller和Admins_controller

I am soooo confused. 我很困惑。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Cheers, 干杯,

You can generate three different devise models, views and controllers for Users , Admins and Vendors . 您可以为UsersAdminsVendors生成三种不同的设计模型,视图和控制器。 You can also add devise helpers to your existing models and controllers instead. 您也可以改为将设计助手添加到现有模型和控制器中。 For views, make sure to use Devise's role-scoped views generator: 对于视图,请确保使用Devise的角色范围视图生成器:

`rails generate devise:views users` # change `users` to whichever role you are generating

Then you can route each role to its own signup in routes.rb using their devise_for helper like so: 然后,您可以使用它们的devise_for助手将每个角色路由到routes.rb自己的signuproutes.rb所示:

devise_scope :user do 
  get 'users/login'   to: 'users#sessions#new',  as 'user_login'
end

devise_scope :admin do 
  get 'admins/login',  to: 'admins#sessions#new', as 'admin_login'
end

devise_scope :vendor do
  get 'vendors/login', to: 'vendors#sessions#new', as 'vendor_login'
end

BUT: 但:

This is not very DRY. 这不是很干。 I would strongly suggest rethinking your design and creating a single model for User with a role attribute. 我强烈建议重新考虑您的设计并为具有role属性的User创建一个模型。 Try the CanCan gem . 尝试CanCan宝石 Here is CanCan's wiki on how to create role-based authorization: https://github.com/ryanb/cancan/wiki/Role-Based-Authorization 这是CanCan关于如何创建基于角色的授权的Wiki: https : //github.com/ryanb/cancan/wiki/Role-Based-Authorization

Okay, several things to look at here. 好的,这里有几件事要看。


STI STI

First step is to get your models sorted out. 第一步是整理模型。

You mention you want to have Users , Vendors and Admin . 您提到要拥有“ Users ,“ Vendors和“ Admin I am presuming you've made several models for this, and would like to stipulate now that it will be much more beneficial for you to inherit from one model. 我想您已经为此创建了多个模型,并且现在要规定,从一个模型继承将对您更加有利。

The single model you need to use should be User , and should extrapolate its functionality through an STI (Single Table Inheritance) : 您需要使用的单个模型应该是User ,并且应该通过STI(单表继承)推断其功能:

#app/models/user.rb
class User < ActiveRecord::Base
   #columns id | type | devise | attributes | created_at | updated_at
end

#app/models/admin.rb
class Admin < User
   #No table necessary
end

#app/models/vendor.rb
class Vendor < User
   #No table necessary
end

This will allow you to use each model as if it were completely standalone, yet maintaining the flexibility of extra functionality. 这样,您就可以像完全独立一样使用每个模型,同时又可以保持额外功能的灵活性。 I can explain more about it if you need; 如果需要的话,我可以进一步解释。 bottom line is that you'll be FAR better off using this than separate tables. 最重要的是,与单独使用表格相比,使用它会更好。


Views 查看

Next we have the views themselves. 接下来我们有自己的看法。

The options you have for your views are as follows: 您的视图选项如下:

  1. Manage the selection with your routes & controllers 使用您的路线和控制器管理选择
  2. Manage the selection in the views themselves 在视图本身中管理选择

I would personally keep a single controller and use different views. 我个人将保留一个控制器并使用不同的视图。 You're using the same data, so all you need to is to either render different layouts , or different views entirely... 您使用的是相同的数据,因此所需要做的就是渲染不同的布局或完全渲染不同的视图 ...

And, by the way, remember that Devise uses its own controllers . 而且,顺便说一下,请记住Devise 使用其自己的控制器 For your purposes, these are Sessions and Registrations respectively: 为了您的目的,分别是SessionsRegistrations

#config/routes.rb
def devise_path_names
    { sign_in: "login", sign_up: "", registration: "register", sign_out: "logout" }
end

devise_for :users,  path_names: devise_path_names #-> url.com/users/login
devise_for :admins, controllers: { sessions: "devise#sessions", registrations: "devise#registrations" }, path_names: devise_path_names #-> url.com/admins/login
devise_for :vendors, controllers: { sessions: "devise#sessions", registrations: "devise#registrations" }, path_names: devise_path_names #-> url.com/vendors/login

The above routes are untested. 以上路线未经测试。

It would be inadvisable to use your own controllers for the respective models - you'll have to recreate them each time, making it very WET . 建议不要将自己的控制器用于相应的模型-您每次都必须重新创建它们,使其非常湿润 Instead, you'd be able to potentially do something like this: 相反,您将可以执行以下操作:

#app/controllers/devise/sessions_controller.rb
class SessionsController < Devise::SessionsController
   before_action :set_route

   private

   def set_route
      @route = request.path.split('/')[0]
   end
end

This would pass the params you require to the devise#sessions controller. 这会将您需要的参数传递给devise#sessions控制器。

Having said that, you could even just change the styling with the CSS classes in the views themselves: 话虽如此,您甚至可以在视图本身中使用CSS类更改样式:

$ rails generate devise:views users

#app/views/devise/sessions/new.html.erb
<%= form_for resource, as: resource_name, url: session_path(resource_name), class: @route do |f| %>
  ...

This will allow you to style the form as required in the CSS. 这将允许您根据CSS的样式设置表单的样式。 It will pull the @route param through the url, allowing you to set the styling in the view. 它将通过网址拉出@route参数,从而允许您在视图中设置样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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