인증 및 권한 부여에 개발자 및 cancan을 사용하는 앱을 만들었습니다. cancan을 사용하여 admin과 operator라는 두 가지 역할을 정의했습니다. 관리자는 모두를 관리 할 수 있으며 운영자는 모두 삭제할 수 있지만 삭제할 수는 없으며 세 번째 사용자는 생성하고 관리 할 수있는 일반 사용자입니다. 그러나 코드는 기본 else 블록에만 적용됩니다. 이는 내 능력 클래스와 index.html을레일스 : Cancan 관련 문제
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
if user.role? :admin
can :manage, :all
elsif user.role? :operator
can :read, :all
else
can :read, :all
end
end
end
index.html을하여 잠시 설정에 따라
<h1>Listing todos</h1>
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @todos.each do |todo| %>
<tr>
<td><%= todo.name %></td>
<td><%= todo.description %></td>
<% if can? :show, @todo %>
<td><%= link_to 'Show', todo %></td>
<% end %>
<% if can? :update, @todo %>
<td><%= link_to 'Edit', edit_todo_path(todo) %></td>
<% end %>
<% if can? :destroy, @todo %>
<td><%= link_to 'Destroy', todo, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>
</table>
<br />
<% if can? :destroy, @todo %>
<%= link_to 'New Todo', new_todo_path %>
<% end %>
'user.rb'에서'role? '메서드를 보여줄 수 있습니까? – MikDiet