【发布时间】:2014-12-21 04:22:24
【问题描述】:
我有一个 Devise User 模型,其属性 admin 类型为布尔值。如果 admin = true,我如何在我的控制器中指定只希望某些操作可用?以下授权方法是否有效?
def authorize
redirect_to login_url, alert:"Not authorized" if current_user.admin == false
end
这是我的 Users 表,admin 是一个布尔值。
create_table "users", force: true 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"
t.datetime "updated_at"
t.boolean "admin", default: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.string "name"
end
使用 Pry 的 authorize_admin 方法中 current_user 的输出:
NameError: undefined local variable or method `current_user' for :authorize_admin:Symbol
我正在使用设计,所以 current_user 不应该工作吗?
这是控制台中 User.first 的输出:
#<User id: 1, email: "email@gmail.com", encrypted_password:
"$2a$10$RCgzx7PJho4vegbf8Z04eumTGB1RyDl5YvDeCAMz7g3...", reset_password_token: nil,
reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 5,
current_sign_in_at: "2014-10-25 23:42:17", last_sign_in_at: "2014-10-23 00:57:07",
current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at:
"2014-10-10
19:24:39", updated_at: "2014-10-25 23:42:17", admin: false, image_file_name: nil,
image_content_type: nil, image_file_size: nil, image_updated_at: nil, name: nil>
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4