0
我正在使用Opinio gem來處理對我的某個型號的評論。我也使用cancan進行authroization。如何用Opinio刪除/銷燬評論寶石
我可以添加評論,沒有問題,但我無法弄清楚如何刪除評論。我簡單地得到一個文字字符串說「未經授權」。而已。
這裏是我的渲染代碼:
<!-- _comment.html.erb (generated by Opinio) -->
<% reply = defined?(reply) ? reply : false %>
<dt id="comment_<%= comment.id %>"><%= link_to comment.owner.name, comment.owner %></dt>
<dd class="well">
<%= simple_format(comment.body) %>
<% if can? :delete, comment%>
<%= link_to t('opinio.actions.delete'), comment_path(comment), :method => :delete%>
<% end %>
<% if Opinio.accept_replies && !reply %>
<span><%= link_to t('opinio.actions.reply'), reply_comment_path(comment), :remote => true %></span>
<ul id="comment_<%= comment.id %>_replies" class="replies">
<%= render :partial => "opinio/comments/comment", :collection => comment.comments, :locals => {:reply => true} %>
</ul>
<% end %>
</dd>
奇怪的是,我最初的if can? :delete, comment
的作品,所以我得到的刪除鏈接起來。我在這裏刪除了一個:remote => true
,這樣我就可以看到實際發生了什麼。
這是我ability.rb:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.has_role? :admin
can :manage, :all
can :access, :rails_admin # grant access to rails_admin
can :dashboard
else
can :read, :all
can :delete, Comment, :owner_id => user.id
end
end
end
我在opinio的source code看了看,我發現,我認爲這是失敗的試驗:
#In opinio gem: comments_controller.rb
if can_destroy_opinio?(@comment)
任何幫助將是巨大的。