简体   繁体   English

如何通过设计,cancan和rolify为用户分配角色?

[英]How to assign roles to a user with devise, cancan and rolify?

i'm new to Ruby on Rails and i'm getting this error: 我是Ruby on Rails的新手,但遇到此错误:

in `const_get': uninitialized constant Devise::Models::RoleId (NameError)

when i remove this ", :role_ids" from my models/user.rb the application works, but i can't assign the roles to my users.. 当我从我的模型/user.rb中删除此“,:role_ids”时,该应用程序可以运行,但是我无法将角色分配给用户。

my models/role.rb 我的模型/role.rb

class Role < ActiveRecord::Base
    has_and_belongs_to_many :users, :join_table => :users_roles
    belongs_to :resource, :polymorphic => true

    scopify
end

my routes: 我的路线:

Rails.application.routes.draw do
    resources :users
    root :to => 'home#index'

    devise_for :users, :skip => [:registrations, :sessions]

    as :user do
        get "/login" => "devise/sessions#new", :as => :new_user_session
        post "/login" => "devise/sessions#create", :as => :user_session
        delete "/logout" => "devise/sessions#destroy", :as => :destroy_user_session
    end
end

and my models/user.rb 和我的模型/ user.rb

class User < ActiveRecord::Base
    rolify
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :role_ids
    validates :email, :uniqueness => true
end

and my UsersController: 和我的UsersController:

class UsersController < ApplicationController
    load_and_authorize_resource
    def index
        @users = User.all
    end

    def show
        @user = User.find(params[:id])
    end

    def new
        @user = User.new
    end

    def edit
        @user = User.find(params[:id])
    end

    def create
        @user = User.new(user_params)

        if @user.save
            redirect_to @user, :flash => { :success => 'User was successfully created.' }
        else
            render :action => 'new'
        end
    end

    def update
        @user = User.find(params[:id])

        if params[:user][:password].blank?
            params[:user].delete(:password)
            params[:user].delete(:password_confirmation)
        end

        params[:user][:role_ids] ||= []

        if @user.update_attributes(user_params)
            sign_in(@user, :bypass => true) if @user == current_user
            redirect_to @user, :flash => { :success => 'User was successfully updated.' }
        else
            render :action => 'edit'
        end
    end


    def destroy
        @user = User.find(params[:id])
        @user.destroy
        redirect_to users_path, :flash => { :success => 'User was successfully deleted.' }
    end

    private
    def user_params
        params.require(:user).permit(:username, :email, :password, :salt, :encrypted_password)
        if can? :manage, User
            params.require(:user).permit(:username, :email, :role_ids, :salt, :encrypted_password)
        else
            params[:user].delete(:role_ids)
            params[:user].delete(:email)
        end
    end
end

i followed this two tutorials: http://danielboggs.com/articles/rails-authentication-and-user-management-via-crud/ and http://danielboggs.com/articles/rails-authentication-and-user-management-continued/ 我遵循了这两个教程: http : //danielboggs.com/articles/rails-authentication-and-user-management-via-crud/http://danielboggs.com/articles/rails-authentication-and-user-management -继续/

I found the problem. 我发现了问题。

the solution is: 解决方案是:

params.require(:user).permit(:username, :email, :password, :salt, :encrypted_password, role_ids: [])

and not: 并不是:

params.require(:user).permit(:username, :email, :password, :salt, :encrypted_password, role_ids)

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

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