【问题标题】:User roles with devise Modular Rails 4设计 Modular Rails 4 的用户角色
【发布时间】:2016-04-21 21:36:05
【问题描述】:

我已经为这个问题苦苦挣扎了好几个星期。我的目标是创建 3 种不同类型的用户。常规,页面,场地。注册后,用户可以选择该角色[常规、页面或地点]。根据用户选择的角色,他们将被重定向到编辑个人资料页面,他们必须在其中填写其他信息。根据用户角色,还为用户提供特定的布局和单独的根。

示例:用户在注册后通过注册表单选择角色,用户将被重定向到他们的编辑表单以填写其他信息,例如姓名、位置。 (还试图找到一种方法来实现这一点,比如在用户可以与平台交互之前,他们必须填写注册表单上不需要的其他信息。任何关于这方面的想法都会很棒。)

所以基本上我想按角色将用户 root 到特定视图。

我的应用程序也在引擎中构建,使用模块化 wa

我现在拥有的:

按照本教程,我能够非常简单地设置用户角色。并且还使用康康舞环绕我的头。 http://www.mashpy.me/rails/role-based-registration-with-devise-and-cancan-using-ruby-on-rails/

用户.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

应用程序控制器

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

注册控制器

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

路线

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

抱歉,如果我遗漏了一些信息,请告诉我需要澄清的地方,我将不胜感激。

【问题讨论】:

    标签: ruby-on-rails devise roles rails-engines modular


    【解决方案1】:

    您可以将 Rolify gem 与 Devise 和 Cancan 或 Cancancan 一起使用来管理多个基于角色的授权。

    通过一些努力,您也应该能够创建和管理角色。

    【讨论】:

    • 感谢您的回答,我将研究 Cancancan 以获得基于多个角色的授权,让您知道我的想法。
    【解决方案2】:

    使用康康宝石 这是它的链接,文档很简单 https://github.com/ryanb/cancan

    【讨论】:

      猜你喜欢
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 2011-06-25
      • 2015-02-22
      • 2015-11-28
      • 1970-01-01
      • 2021-04-01
      相关资源
      最近更新 更多