Ruby on rails 3.2 cancan中的检查能力
我是一个相对的新手,我试图让cancan在一个简单的项目中工作。我已经阅读了Railscast并阅读了文档,但是我遗漏了一些东西 我有两个问题Ruby on rails 3.2 cancan中的检查能力,ruby-on-rails-3.2,authorization,cancan,Ruby On Rails 3.2,Authorization,Cancan,我是一个相对的新手,我试图让cancan在一个简单的项目中工作。我已经阅读了Railscast并阅读了文档,但是我遗漏了一些东西 我有两个问题 我为普通用户正确隐藏了编辑/创建链接。但是,它们对管理员用户也是隐藏的 如果我注释掉隐藏上述链接的代码,当管理员用户单击链接时,他们会收到一条拒绝访问的消息,该消息应该显示给非管理员用户 所以我认为我的问题是应用程序没有正确地检查功能。我已经检查了数据库,并且该用户的“角色”列正确填充了“管理员” 以下是我的用户模型: class User < A
class User < ActiveRecord::Base
has_secure_password
#this is for authentication
attr_accessible :email, :password, :password_confirmation, :role
validates_uniqueness_of :email
#end of authentication
end
根据角色隐藏控件的我的视图我有以下代码:
<% if can? :manage, Instructor %>
<td><%= link_to 'Edit', edit_instructor_path(instructor) %></td>
<td><%= link_to 'Destroy', instructor, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
非常感谢您的帮助
谢谢
斯科特而不是这个
<% if can? :manage, Instructor %>
试试这个
<% if user.has_role? :admin %>
(假设在你的应用程序中,管理员管理/拥有比讲师更多的权限。)我更改了:
if user.admin?
到
它工作得很好
if user.admin?
if user.id