Ruby on rails 如何检查“共享/部分”中对象的CanCan能力?
根据Ruby on rails 如何检查“共享/部分”中对象的CanCan能力?,ruby-on-rails,ruby-on-rails-4,authorization,cancan,Ruby On Rails,Ruby On Rails 4,Authorization,Cancan,根据CanCan文档,要检查用户是否有能力对视图中的任何元素执行操作,请执行以下操作: <% if can? :create, @project %> <%= link_to "New Project", new_project_path %> <% end %> 在我的视图/dashboard/index.html.erb中,我有以下内容: <% @nodes.each do |node| %> <
CanCan
文档,要检查用户是否有能力对视图中的任何元素执行操作,请执行以下操作:
<% if can? :create, @project %>
<%= link_to "New Project", new_project_path %>
<% end %>
在我的视图/dashboard/index.html.erb
中,我有以下内容:
<% @nodes.each do |node| %>
<!-- Upload Video Comment Popup -->
<div class="box">
<%= render partial: "shared/comments", locals: {node: node} %>
</div>
<% end %> <!-- node -->
<% if node.comments.present? %>
<% node.comments.each do |comment| %>
<% if can? :manage, Comment %>
Show Something Interesting Here
<% else %>
Show something boring here
<% end %>
<% end %>
<% end %>
那不行
我也试过:
<% if node.comments.present? %>
<% node.comments.each do |comment| %>
<% if can? :manage, comment %>
Show Something Interesting Here
<% else %>
Show something boring here
<% end %>
<% end %>
<% end %>
我曾考虑在控制器中创建一个@comments
实例变量,但问题是comments
位于一组节点上(即,我需要显示多个节点,每个节点都有多个注释)
如何处理此问题?在更新到CanCanCommunity版本的cancan后,您的上一个代码应该可以正常工作
<% if node.comments.present? %>
<% node.comments.each do |comment| %>
<% if can? :manage, comment %>
Show Something Interesting Here
<% else %>
Show something boring here
<% end %>
<% end %>
<% end %>
在这里展示一些有趣的东西
在这里展示一些无聊的东西
与帅哥有关。你今天表现很好!
<% if node.comments.present? %>
<% node.comments.each do |comment| %>
<% if can? :manage, comment %>
Show Something Interesting Here
<% else %>
Show something boring here
<% end %>
<% end %>
<% end %>
can :manage, Comment, user_id: user.id
<% if node.comments.present? %>
<% node.comments.each do |comment| %>
<% if can? :manage, comment %>
Show Something Interesting Here
<% else %>
Show something boring here
<% end %>
<% end %>
<% end %>