簡體   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