gpt4 book ai didi

ruby-on-rails - 未定义的方法 `admin?' 与 Cancan

转载 作者:数据小太阳 更新时间:2023-10-29 08:57:12 24 4
gpt4 key购买 nike

我在查找如何检查 CanCanCan 中的管理员权限时遇到了问题。

  • if user.admin? 我得到未定义的方法
  • if user.is? 我得到未定义的方法
  • if user.has_attribute?(:admin)
  • if user.user_type == "admin" 我得到未定义的方法

我对 has_attribute 抱有一些希望,但它无济于事,即使我没有收到警报。 puts 'hey' 在控制台中证明了这一点。

我一个月前开始学习 Rails,由于 Windows 的原因,我遇到了一些限制。有没有可能我的问题是因为windows导致的?

On the other hand, if user.present? works and it gives some hopes again.

我的用户模型:

class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts, dependent: :destroy

end

和数据库字段

  create_table "active_admin_comments", force: :cascade do |t|
t.string "namespace"
t.text "body"
t.string "resource_type"
t.integer "resource_id"
t.string "author_type"
t.integer "author_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id"
t.index ["namespace"], name: "index_active_admin_comments_on_namespace"
t.index ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id"
end

create_table "admin_users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_admin_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true
end

create_table "posts", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.integer "author_id"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

来自 application_controller.rb

def access_denied(exception)
respond_to do |format|
format.json { head :forbidden, content_type: 'text/html' }
format.html { redirect_to main_app.root_url, notice: exception.message }
end
end

编辑

I thought for a while that the code suggested by @mrzasa would bring a solution as I had no more alert. This was because of my ability.rb :

if user.present?
if user.admin?
puts 'hey admin'
can :manage, :all
end
can :read, all
can :manage, Post, :author_id => user.id
puts 'hey user'
end

If I comment # if user.present? the alert undefined method 'admin?'comes back again. A proof that user.present works, but here to say that there is no user, until I log in outside of the admin panel as a user and then I can see the puts in the console. But I can't perform any action, unless I state can :manage, :all to ANY user.

在这个阶段,我添加了 user ||= User.new 以在检查管理员之前创建一个用户实例。即使我允许任何访问者以管理员身份登录,user.admin?永远不会验证,除非我在 user.rb 中将 def admin? 设置为 true

我看到许多使用 Cancancan 的人改为定义角色。也许我应该去做...

编辑 2

It works! I worked on it again from the install of Cancancan to the point where I was with the additions of @mrzasa. This time, active admin understands admin? from the class AdminUser which was not the case yesterday. The beautiful thing is that I did not change any line of code, except commenting # user ||= User.new to get the expected results.

最佳答案

您似乎有两个独立的模型 - 一个用于普通用户(users 表),另一个用于管理员(admin_users 表)。您可以为它们添加 admin? 方法 - 对于返回 false 的用户和返回 true 的 admin。

class User < ApplicationRecord
# ...
def admin?
false
end
end

class AdminUser < ApplicationRecord
# ...
def admin?
true
end
end

关于ruby-on-rails - 未定义的方法 `admin?' 与 Cancan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49513572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com