我設置cancancan來實現此功能: 如果用戶是管理員,它可以銷燬除自己以外的每個用戶。 這是我ability.rb安裝cancancan用戶無法自行銷燬
class Ability
include CanCan::Ability
def initialize(user)
if user.role == 'admin'
cannot :destroy, User, id: user.id
end
end
end
這是我的看法
<h1>Listing Users</h1>
<table>
<thead>
<tr>
<th>E-Mail</th>
<th>Name</th>
<th>Role</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.email %></td>
<td><%= user.name %></td>
<td><%= user.role %></td>
<% if can? :destroy, @user %>
<%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
</tr>
<% end %>
</tbody>
</table>
即使在此設置有沒有現在每個用戶的最後破壞鏈接。我想要的是每個用戶背後都有一個銷燬鏈接,但管理員本身。我該怎麼辦?謝謝!
仍然不讓我刪除用戶 – DevArenaCN