【问题标题】:devise+cancan don't recognize abilitydevise+can 无法识别能力
【发布时间】:2016-10-02 15:45:20
【问题描述】:

我有一个问题,当我使用 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"

有人知道如何解决吗?或者有人刚刚发生了

提前致谢!

【问题讨论】:

  • 您需要告诉我们您执行了哪些导致错误的步骤,并自己采取一些基本的调试步骤。就像检查 current_user 返回的内容一样。

标签: ruby-on-rails ruby-on-rails-4 devise cancan


【解决方案1】:

能力定义中的类名错误。

如下更改ability.rb

class Ability
  include CanCan::Ability

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

【讨论】:

  • 谢谢,上帝保佑你!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 2021-01-06
  • 1970-01-01
  • 2017-06-05
  • 2022-09-16
  • 1970-01-01
相关资源
最近更新 更多