2016-10-02 2 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

ありがとう、神よ、あなたを祝福しなさい! –

関連する問題