2016-10-02 68 views
0

我遇到問題,當我用cancan管理用戶時,表示「您無權訪問此頁面」。 這裏是我的代碼:devise + cancan無法識別能力

class Ability 
    include CanCan::Ability 

    def initialize(user) 


    if user.tipo == 'c' 

     can :manage, :User 

    end 
    end 
end 

,這裏是我的控制器用戶

class UserController < ApplicationController 
    load_and_authorize_resource 

    def index 
    @users = User.excludes(:id => current_user.id) 
    end 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(params[:user]) 
    if @user.save 
     flash[:notice] = "Successfully created User." 
     redirect_to root_path 
    else 
     render :action => 'new' 
    end 
    end 

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

    def update 
    @user = User.find(params[:id]) 
    params[:user].delete(:password) if params[:user][:password].blank? 
    params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank? 
    if @user.update_attributes(params[:user]) 
     flash[:notice] = "Successfully updated User." 
     redirect_to root_path 
    else 
     render :action => 'edit' 
    end 
    end 

    def destroy 
    @user = User.find(params[:id]) 
    if @user.destroy 
     flash[:notice] = "Successfully deleted User." 
     redirect_to root_path 
    end 
    end 
end 

,在這裏我的路線

Rails.application.routes.draw do 
    resources :proyects 
    devise_for :users, :controllers => {:registrations => "users/registrations"} 
    resources :users, :controller => "user" 

一些身體知道如何解決它?或有人剛剛發生

在此先感謝!

+0

你需要告訴我們什麼步驟,你是領導到錯誤,並把自己的一些基本的調試步驟。像檢查'current_user'返回什麼。 – max

回答

1

能力定義中的類名是錯誤的。

變化ability.rb,如下所示:

class Ability 
    include CanCan::Ability 

    def initialize(user) 
    if user.tipo == 'c' 
     can :manage, User 
    end 
    end 
end 
+0

謝謝,上帝保佑你! –