简体   繁体   English

devise Modular Rails 4的用户角色

[英]User roles with devise Modular Rails 4

I've been struggling with this issue for weeks. 数周来我一直在努力解决这个问题。 My goal is to create 3 different types of users. 我的目标是创建3种不同类型的用户。 Regular, Page , Venue. 常规,页面,地点。 Upon registration the user is able to select that role [Regular,Page or Venue]. 注册后,用户可以选择该角色[常规,页面或地点]。 depending on the role the user chooses they're redirected to a edit profile page where they have to fill in additional information. 根据用户选择的角色,他们将被重定向到编辑个人资料页面,在该页面中,他们必须填写其他信息。 Depending on user role, the user is also provided with a specific layout and separate root. 根据用户角色,还为用户提供了特定的布局和单独的根。

Example : User chooses role via registration form once signed up the user is redirected to their edit form to fill in additional info like name, location. 示例:注册后,用户通过注册表单选择角色,用户将被重定向到其编辑表单以填写其他信息,例如姓名,位置。 ( also trying to find a way to make that required, like before a user can interact with the platform they have to fill in additional information that was not required on the registration form. Any ideas on something for that would be amazing.) (还要尝试找到一种方式来实现这一要求,例如,在用户与平台进行交互之前,他们必须填写注册表格中不需要的其他信息。关于此的任何想法都将令人惊讶。)

so basically I would like to root the user by role to a specific view. 所以基本上我想按角色将用户定向到特定视图。

Also my application is being built within engines, using the modular wa 另外,我的应用程序是使用模块化wa在引擎内构建的

What I have now : 我现在所拥有的:

By following this tutorial I was able to set up the user roles very simply. 通过遵循本教程,我可以非常简单地设置用户角色。 and also wrapping my head around using cancan. 并用康康舞把我的头缠住。 http://www.mashpy.me/rails/role-based-registration-with-devise-and-cancan-using-ruby-on-rails/ . http://www.mashpy.me/rails/role-based-registration-with-devise-and-cancan-using-ruby-on-rails/

User.rb User.rb

module M0ve
class User < ActiveRecord::Base

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

validates :user_name, presence: true, length: { minimum: 4, maximum: 12 }
validates :email, presence: true
validates :role, presence: true

has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy

ROLES = %w[regular page venue]

def role_symbols
      [role.to_sym]
end

Application Controller 应用控制器

module M0ve
  class ApplicationController < ActionController::Base
      before_filter :authenticate_user!
 protect_from_forgery with: :exception

  protected
  def after_sign_up_path_for(resource)
      if current_user.role? :regular 
        regular_index

      end
  end



  end
end

Registration Controller 注册控制器

module M0ve
class M0ve::RegistrationsController < Devise::RegistrationsController

 private

  def sign_up_params
    params.require(:user).permit(:email, :user_name, :password, :password_confirmation, :role)
  end

  def account_update_params
    params.require(:user).permit(:email, :user_name, :password, :password_confirmation, :current_password)
  end
end
end

Routes 路线

M0ve::Core::Engine.routes.draw do

  devise_for :users, class_name: "M0ve::User",module: :devise,:controllers => { registrations: 'm0ve/registrations' }


 resources :posts do  
 resources :comments
end 
end

Sorry if I missed some information please let me know where I need to clarify and I'll appreciate any help . 对不起,如果我错过了一些信息,请告诉我需要澄清的地方,我们将不胜感激。

You could use the Rolify gem with Devise and Cancan or Cancancan to manage multiple role based authorization. 您可以将Rolify gem与Devise和Cancan或Cancancan一起使用来管理基于多个角色的授权。

With some effort you should be able to create and manage roles as well. 通过一些努力,您也应该能够创建和管理角色。

使用Cancan Gem是这里的链接,文档非常简单https://github.com/ryanb/cancan

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

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