CANCAN 未定义方法“user_type?”对于“admin”:字符串

发布于 2024-11-07 00:17:40 字数 783 浏览 1 评论 0原文

这是我的问题:

我正在使用 cancan gem。我认为一切都设置得很好,除了一件事,它会产生未定义的方法“user_type?”对于“admin”:字符串。

这是我的能力.rb

class Ability
  include CanCan::Ability
  def initialize(user)
       user ||= User.new # guest user
       if user.user_type? :admin
         can :manage, :all
       # [code code code code commented to find more easily the problem]
        else
          can :read, :all
        end
  end
end

我的用户.rb

require 'digest/sha2'

class User < ActiveRecord::Base
  #definisco i ruoli
  ROLES = %w[admin client banned]
  default_scope :order => 'user' [...]

我不知道我必须修改什么。为什么如果是user.user_type? :admin 不检查当前用户属性?非常感谢您的关注!

UP:好的,我读到 cancan 需要在应用程序控制器中声明方法“current_user”。现在我正在努力。消息,快了。

here is my problem:

I'm using cancan gem. I think all is set up well, except for one thing, that produces undefined method 'user_type?' for "admin":String.

Here is my ability.rb

class Ability
  include CanCan::Ability
  def initialize(user)
       user ||= User.new # guest user
       if user.user_type? :admin
         can :manage, :all
       # [code code code code commented to find more easily the problem]
        else
          can :read, :all
        end
  end
end

My user.rb

require 'digest/sha2'

class User < ActiveRecord::Base
  #definisco i ruoli
  ROLES = %w[admin client banned]
  default_scope :order => 'user' [...]

I can't figure out what I have to modify. Why if user.user_type? :admin don't check the current user attribute? Thanks a lot for your attention!

UP: Ok, I read around that cancan need method "current_user" declared in application controller. Now I work on it. News, soon.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦毁影碎の 2024-11-14 00:17:40

你应该这样做。我认为这是更好的方法。

将种子角色植入数据库。

并在应用程序帮助器中粘贴此代码。

def is_admin?(user)
  admin_role = Role.find(:first, :conditions => ["name = ?", "admin"])
  return user.roles.include?(admin_role)
end

然后在能力调用中

            class Ability
             include CanCan::Ability
             def initialize(user)
               user ||= User.new # guest user
                 if is_admin?(user)
                   can :manage, :all
                    # [code code code code commented to find more easily the problem]
                 else
                   can :read, :all
                 end
             end
           end

,它会正常工作......

You should do like this.I think it is better approach.

Seed roles into DB.

And in application helper paste this code.

def is_admin?(user)
  admin_role = Role.find(:first, :conditions => ["name = ?", "admin"])
  return user.roles.include?(admin_role)
end

And then in ability call like

            class Ability
             include CanCan::Ability
             def initialize(user)
               user ||= User.new # guest user
                 if is_admin?(user)
                   can :manage, :all
                    # [code code code code commented to find more easily the problem]
                 else
                   can :read, :all
                 end
             end
           end

And it will work fine.......

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文