Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 具有rails 4.0的cancancan功能_Ruby On Rails 4_Cancancan - Fatal编程技术网

Ruby on rails 4 具有rails 4.0的cancancan功能

Ruby on rails 4 具有rails 4.0的cancancan功能,ruby-on-rails-4,cancancan,Ruby On Rails 4,Cancancan,我在rails应用程序中使用cancancan身份验证机制。我只希望那些拥有自己的帖子和评论的用户不能被编辑和删除,管理员可以管理所有的事情。我的管理能力很好,但其他人不工作。这是我的ability.rb文件 class Ability include CanCan::Ability def initialize(user) user ||= User.new # guest user if user.admin? can :manage, :all else can :rea

我在rails应用程序中使用cancancan身份验证机制。我只希望那些拥有自己的帖子和评论的用户不能被编辑和删除,管理员可以管理所有的事情。我的管理能力很好,但其他人不工作。这是我的ability.rb文件

class Ability
include CanCan::Ability

def initialize(user)
user ||= User.new # guest user

if user.admin? 
   can :manage, :all

else
  can :read, :all
  can :create, Topic
  can :update, Topic do |topic|
    topic.try(:user) == user
  end
  can :delete, Topic do |topic|
    topic.try(:user) == user
  end
  can :delete, Comment do |comment|
  comment.try(:user) == user
  end
 # can :manage, Comment, :task => { :user_id => user.id }
  can :update, Comment do |comment|
  comment.try(:user) == user

    end
   end
 end
结束 我应该怎么做才能正常工作呢。它可以正确地处理主题,但不能处理评论

这是我的主题控制器中的一行

load_and_authorize_resource :topic

如果它适用于主题,那么您的评论模型肯定有问题。comments表是否有存储注释作者的user_id列,或者是否有任何其他方式来检查其所有权?可能就是那个注释。try:user返回nil,然后它就无法给用户正确的权限