繁体   English   中英

devise Modular Rails 4的用户角色

[英]User roles with devise Modular Rails 4

数周来我一直在努力解决这个问题。 我的目标是创建3种不同类型的用户。 常规,页面,地点。 注册后,用户可以选择该角色[常规,页面或地点]。 根据用户选择的角色,他们将被重定向到编辑个人资料页面,在该页面中,他们必须填写其他信息。 根据用户角色,还为用户提供了特定的布局和单独的根。

示例:注册后,用户通过注册表单选择角色,用户将被重定向到其编辑表单以填写其他信息,例如姓名,位置。 (还要尝试找到一种方式来实现这一要求,例如,在用户与平台进行交互之前,他们必须填写注册表格中不需要的其他信息。关于此的任何想法都将令人惊讶。)

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

另外,我的应用程序是使用模块化wa在引擎内构建的

我现在所拥有的:

通过遵循本教程,我可以非常简单地设置用户角色。 并用康康舞把我的头缠住。 http://www.mashpy.me/rails/role-based-registration-with-devise-and-cancan-using-ruby-on-rails/

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

应用控制器

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

对不起,如果我错过了一些信息,请告诉我需要澄清的地方,我们将不胜感激。

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

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

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

暂无
暂无

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

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