我在使用 CanCan gem 时遇到了一些问题。
我有能力.rb文件:
if user.nil?
can :read, :all
elsif user.admin?
can :manage, Publication
else
can [:read, :create], Publication
can [:update, :destroy], Publication, :user_id => user.id
end
它是publication.rb:
attr_accessible :content,:title
belongs_to :user
validates :user_id, presence: true
validates :title, presence: true, length: { maximum: 140 }
validates :content, presence: true, length: { minimum: 240 }
default_scope order: 'publications.created_at DESC'
它是用于出版物的 index.html.erb:
<% @publications.each do |publicate| %>
<h3><%= publicate.title %></h3>
<% if can? :update, :destroy, Publication %>
<%= link_to "Update", edit_publication_path(publicate) %>
|<%= link_to " delete", publicate, method: :delete,
data: { confirm: "Are you sure?" } %>
<% end %>
<% end %>
它不显示delete
和Update
链接,如果它是管理员或登录用户。但如果我改变ability.rb
:
elsif user.admin?
can :manage, Publication
Publication
to User
,它有效,我在用户视图中看到链接删除:
<% @users.each do |user| %>
<li>
<%= link_to user.username, user %>
<% if can? :destroy, user %>
| <%= link_to "delete", user, method: :delete,
data: { confirm: "Are you sure?" } %>
<% end %>
</li>
<% end %>
而且user.admin can :manage, :all
,它也适用于用户和出版物。为什么CanCan可以忽略Publication能力?