[英]How to handle inflections with CanCanCan
我有ContactUsController
和ContactUs
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.