簡體   English   中英

如何使用 CanCanCan 處理變形

[英]How to handle inflections with CanCanCan

我有ContactUsControllerContactUs model。
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”的詞形變化為不可數

通過編輯inflections.rb解決

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM