2014-09-30 87 views
1

我是新來的Ruby on Rails和我得到這個錯誤:從我的模型/ user.rb應用程序的工作,但我不能:如何使用devise,cancan和rolify將角色分配給用戶?

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

當我刪除這個「role_ids」這些角色分配給我的用戶..

我的模型/ role.rb

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

    scopify 
end 

我的路線:

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 

個我的模型/ 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 

和我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 

我跟着這兩個教程: http://danielboggs.com/articles/rails-authentication-and-user-management-via-crud/http://danielboggs.com/articles/rails-authentication-and-user-management-continued/

+0

做你試過的has_many:在用戶模式的角色? – knotito 2014-09-30 10:57:10

+0

@knotito是的,我得到這個錯誤:SQLite3 :: SQLException:沒有這樣的列:roles.user_id:SELECT COUNT(*)FROM「roles」WHERE「roles」。「user_id」=? AND(((roles.name ='admin')AND(roles.resource_type IS NULL)AND(roles.resource_id IS NULL))) – Evolutio 2014-09-30 12:00:21

回答

1

我發現這個問題。

的解決方案是:

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

,而不是:

params.require(:user).permit(:username, :email, :password, :salt, :encrypted_password, role_ids) 
相關問題