Rails 4:cancan无法查看编辑或删除评论

时间:2015-05-24 07:43:20

标签: ruby-on-rails ruby-on-rails-4 cancan cancancan

使用cancan无法在评论中查看编辑或删除后,评论与作业相关。

user ||= User.new # guest user (not logged in)
        if user.admin?
            can :access, :rails_admin       # only allow admin users to access Rails Admin
            can :dashboard
            can :manage, :all
        else
            can :read, :all
            can [ :edit, :update, :destroy ], Comment do |comment|
                comment.try(:user_id) == user.id
            end

            can [ :edit, :update, :destroy ], Job do |job|
                job.user_id == user.id
            end
        can :create , Comment
        can :create , Job
        end

- if can? :update, @comment
	= link_to "Edit", edit_job_comment_path(comment.job, comment)

- if can? :destroy, @comment
	= link_to "Delete", [comment.job, comment], method: :delete, data: { confirm: "Are you sure?" }

1 个答案:

答案 0 :(得分:-1)

尝试以下代码:

can [ :edit, :update, :destroy ], Comment, :user_id => user.id
can [ :edit, :update, :destroy ], Job, :user_id => user.id