简体   繁体   中英

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

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/ .

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.

With some effort you should be able to create and manage roles as well.

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

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