【问题标题】:Cancancan create authorization still lets create form to be shown to other usersCancancan 创建授权仍然允许创建表单显示给其他用户
【发布时间】:2019-08-17 20:37:31
【问题描述】:

其他表单的所有其他授权都可以正常工作。但是对于下面的这个创建表单,授权一直让其他人看到这个表单。

<%= form_for([@book]) do |form| %>

授权码:

<% if can? :create, @book %>

当我将 @book 切换到 Book 或 [@user, @book] 或 [@book, @post] 时,没有任何效果。 @user,@book 对当前用户和其他用户都隐藏表单。

我能做什么?

能力.rb:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new
    if user.present?
      can :read, Book
      can :read, Post
      can :read, Comment
      can :manage, Book, user_id: user.id
      can :manage, Post, book: { user: { id: user.id } }      
      can :manage, Comment, post: { book: { user: { id: user.id }}}

    end
```

【问题讨论】:

  • 你能给我们展示你的能力档案吗?
  • @morissetcl 当然 - 更新了。

标签: ruby-on-rails cancancan


【解决方案1】:

好的,我认为你的错误在这里:

can :manage, Book, user_id: user.id

基本上在create 操作中,您的对象还没有user_id,因为您的对象尚未保存。因此,您应该使用以下代码更新您的能力文件:

can [:read, :create], Book
can [:destroy, :update], Book, user_id: user. id

告诉我。

编辑

好的,我错过了一点。你有一个声明来检查你是否有上面的current_user,所以我认为你不需要在你的Book 能力中添加user_id:

你可以试试:can manage, Book

另外一定要重启你的服务器,有时能力可能会反复无常。

【讨论】:

  • 谢谢你。我已经尝试过了,不幸的是它似乎不起作用。所以Book and @book 向两种类型的用户显示表单,@user, @book or User, Book 对两种用户隐藏视图。
  • 谢谢 - 今天晚些时候我会检查代码。抱歉耽搁了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-25
  • 1970-01-01
相关资源
最近更新 更多