【问题标题】:How to handle inflections with CanCanCan如何使用 CanCanCan 处理拐点
【发布时间】:2020-07-17 19:11:19
【问题描述】:

我有 ContactUsControllerContactUs 模型。
contact_us_controller.rb

class ContactUsController < ApplicationController
  skip_before_action :authenticate_request, only: :create
  load_and_authorize_resource

  ...
end

inflections.rb

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.uncountable 'ContactUs'
end

我正在使用 CanCanCan 来处理授权。 在ability.rb 我有

class Ability
  include CanCan::Ability

  def initialize(user)
    unless user.present?
      can :create, ContactUs # guest users can create contact us
    end

    if user.admin?
      can :manage, ContactUs # only admins can do CRUD 
    end
  end
end

我收到以下错误

NameError (uninitialized constant ContactU Did you mean?  ContactUs):

activesupport (6.0.3.2) lib/active_support/inflector/methods.rb:282:in `const_get'
...

我不知道为什么 CanCanCan 会检查 ContactU 而不是 ContactUs!尽管 'ContactUs' 的变形为不可数

【问题讨论】:

  • 尝试重启服务器
  • 我做到了。同样的错误。

标签: ruby-on-rails ruby cancancan


【解决方案1】:

编辑inflections.rb解决

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.uncountable 'contact_us'
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    相关资源
    最近更新 更多