【问题标题】:How to make a user can only read his post?如何让用户只能阅读他的帖子?
【发布时间】:2014-11-11 14:15:43
【问题描述】:

我是 Rails 的初学者。我创建了一个新应用程序,然后脚手架发布,然后安装 devise & cancan。我能找到的教程差不多是这样的:

def initialize(user)
  user ||= User.new
  if user.admin?
    can :manage, Article
  else
    can :read, Article
  end
end

我不希望其他用户可以阅读 my_own 的帖子,我只希望每个用户只能阅读他的帖子。我怎样才能做到这一点?我希望有一个完整的样本,因为我是一个愚蠢的人。非常感谢!

【问题讨论】:

    标签: ruby-on-rails devise cancan


    【解决方案1】:

    你可以传入哈希conditions 例如

    def initialize(user)
      user ||= User.new
      if user.admin?
        can :manage, Article
      else
        can :manage, Article, user_id: user.id
        can :read, Article
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2012-07-09
      • 2015-06-06
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      • 2021-11-03
      相关资源
      最近更新 更多