CanCan 每个用户一个角色,未定义方法“角色”;问题

发布于 2024-12-03 06:14:18 字数 1032 浏览 2 评论 0原文

真的很困惑。角色按照每个用户一个角色的方法设置并正常工作。我的用户模型如下。

class User < ActiveRecord::Base

    ROLES = %w[admin landlord]

    def role?(role)
     roles.include? role.to_s
    end
 end

当我将权限添加到我的能力模型时,我收到以下错误。

undefined method `role' for #<ActionDispatch::Session::AbstractStore::SessionHash:0x10433a5e0>

我的能力模型如下。

class Ability
  include CanCan::Ability

  def initialize(user)
    if user.role == "admin"
      can :manage, :all
    else
     can :read, :all
    end
  end
end

这是我在终端中看到的。

NoMethodError (undefined method `role' for #<ActionDispatch::Session::AbstractStore::SessionHash:0x1044d46a8>):
  app/models/ability.rb:5:in `initialize'
  app/controllers/application_controller.rb:6:in `new'
  app/controllers/application_controller.rb:6:in `current_ability'

正如你所知,我只是在学习!即使朝着正确的方向轻推也会令人惊奇。谢谢。

Really confused. Roles are set up and working nicely following the one role per user method. My user model is below.

class User < ActiveRecord::Base

    ROLES = %w[admin landlord]

    def role?(role)
     roles.include? role.to_s
    end
 end

It is when I add the permissions to my ability model I get the following error.

undefined method `role' for #<ActionDispatch::Session::AbstractStore::SessionHash:0x10433a5e0>

My Ability model is below.

class Ability
  include CanCan::Ability

  def initialize(user)
    if user.role == "admin"
      can :manage, :all
    else
     can :read, :all
    end
  end
end

And here is what I see in terminal.

NoMethodError (undefined method `role' for #<ActionDispatch::Session::AbstractStore::SessionHash:0x1044d46a8>):
  app/models/ability.rb:5:in `initialize'
  app/controllers/application_controller.rb:6:in `new'
  app/controllers/application_controller.rb:6:in `current_ability'

As you can tell I am just learning! Even a nudge in the right direction would be amazing. Thanks.

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

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

发布评论

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

评论(2

甜`诱少女 2024-12-10 06:14:19

您正在将会话传递给 CanCan。您需要传递当前登录的用户。我猜测在您的 Rails 书中的解释中,应该有一种方法可以从变量会话访问该用户。将该用户作为该功能的参数传递。

如果用户未登录,我将发送一个新用户。

您需要将代码重构为:

def current_ability
  if session[:user_id]  # Replace with the method to get the user from your session
    user = User.find(session[:user_id])
  else
    user = User.new
  end

  @current_ability ||= Ability.new(user)
end

You are passing a session to CanCan. You need to pass the user that is currently signed in. Im guessing in the explanation of your rails book, there should be a way to access that user from the variable session. Pass that user as an argument to the ability.

If a user is not signed in, I would send a new User.

You would need to refactored the code, to something like:

def current_ability
  if session[:user_id]  # Replace with the method to get the user from your session
    user = User.find(session[:user_id])
  else
    user = User.new
  end

  @current_ability ||= Ability.new(user)
end
你如我软肋 2024-12-10 06:14:18

问题是您定义了 role? 方法,但在 ability.rb 中您调用了 role 方法,这当然是未定义的

正确方法那将是

def initialize(user)
  if user.role? "admin"
    can :manage, :all
  else
    can :read, :all
  end
end

the problem is that you defined role? method but in ability.rb you call the role method which is of course undefined

the proper way to do that will be

def initialize(user)
  if user.role? "admin"
    can :manage, :all
  else
    can :read, :all
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文