未初始化的恒定能力导轨 [英] uninitialized constant Ability Rails

查看:56
本文介绍了未初始化的恒定能力导轨的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

针对此问题,我已经采用了不同的解决方案,但是没有一个起作用,所以请不要尝试重复解决该问题.

I have gone through different solutions given to this problem but none of them is working so please don't try to close the question as duplicate.

我的用户表中有角色列.因此,用户可以通过 admin user 进行操作,而我需要使用 CanCan 在用户Role的基础上设置权限.我想将所有权限授予管理员.我以管理员身份登录,但是当我访问/users 时,出现错误 uninitialized constant Ability ,而当我删除 load_and_authorize_resource 时,我的cancan权限却没有没事.我的能力课看起来像

I have role column in my users table. So user can by admin or user and I need to put permissions on the base of user Role using CanCan. I want to give all permissions to admin. I am logged in as admin but when I access /users I get the error uninitialized constant Ability and when I remove load_and_authorize_resource my cancan permission doesn't work.My ability class looks like

class Ability
  include CanCan::Ability

  def initialize(user)
    #abort("Message goes here")
    user ||= User.new # guest user
    #abort('some user')
    if user.role == 'admin'
      can :manage, :all
    elsif user.role == 'user'
      can :manage, Micropost do |micropost|
        micropost.try(:owner) == user
      end
      can :update, User do |users|
        users.try(:owner) == user
      end
    else
     can :read, :all
    end
  end
end

在我的 UsersController 中,我正在

class UsersController < ApplicationController
  load_and_authorize_resource
  #devise code
  before_filter :authenticate_user!, only: [:index, :edit, :update, :destroy, :following, :followers]
  blah blah
 end

我的路线文件看起来像

FirstApp::Application.routes.draw do

  devise_for :users

  resources :users do
  member do
    get :following, :followers
  end
end
#resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]

root to: "static_pages#home"

match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
end

推荐答案

您看到的是未初始化的常数A ,因为 UsersController 中的 load_and_authorize_resource 方法>希望找到一个能力类.

You are seeing uninitialized constant Ability because the load_and_authorize_resource method in your UsersController expects to find an Ability class.

解决方案是将包含您的能力定义的文件移动到 app/models/ability.rb .

The solution is to move the file containing your ability definitions to app/models/ability.rb.

#app/models/ability.rb
class Ability
  include CanCan::Ability

  def initialize(user)
    #abort("Message goes here")
    user ||= User.new # guest user
    #abort('some user')
    if user.role == 'admin'
      can :manage, :all
    elsif user.role == 'user'
      can :manage, Micropost do |micropost|
        micropost.try(:owner) == user
      end
      can :update, User do |users|
        users.try(:owner) == user
      end
    else
     can :read, :all
    end
  end
end

这篇关于未初始化的恒定能力导轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆