【问题标题】:CanCan::Error - The can? and cannot? call cannot be used with a raw sql 'can' definitionCanCan::Error - 罐头?不能吗?调用不能与原始 sql 'can' 定义一起使用
【发布时间】:2016-12-22 06:56:50
【问题描述】:

能力.rb

can :destroy, Business, Business.where(id: user.auth_ids)

index.html.erb

<% if can? :destroy, @business %>
  <%= link_to 'delete', business_path(@business.id), method: :delete%>
<% end %>

如果用户字段 auth_ids: [] 包含企业 ID,我想访问删除按钮。
但是我遇到了这样的错误

CanCan::Error - The can? and cannot? call cannot be used with a raw sql 'can' definition.

谁能告诉我问题的原因以及如何解决?

【问题讨论】:

    标签: ruby-on-rails cancan


    【解决方案1】:

    这似乎发生在语法不完全正确时。试试hash with your condition

    # ability.rb
    
    can :destroy, Business, id: user.auth_ids
    

    block condition with your scope:

    # ability.rb
    
    can :destroy, Business, Business.where(id: user.auth_ids) do
      #... additional logic
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 2011-12-28
      • 2021-11-28
      • 2020-10-03
      相关资源
      最近更新 更多