Ruby on rails Heroku上的Rails:NameError:未初始化的常量应用程序策略
将Rails API应用程序部署到Heroku时,我的构建失败,错误如下:Ruby on rails Heroku上的Rails:NameError:未初始化的常量应用程序策略,ruby-on-rails,heroku,pundit,Ruby On Rails,Heroku,Pundit,将Rails API应用程序部署到Heroku时,我的构建失败,错误如下: -----> Detecting rake tasks sh: 2: Syntax error: Unterminated quoted string sh: 2: Syntax error: Unterminated quoted string ! ! Could not detect rake tasks ! ensure you can run `$ bundle exec rake -
-----> Detecting rake tasks
sh: 2: Syntax error: Unterminated quoted string
sh: 2: Syntax error: Unterminated quoted string
!
! Could not detect rake tasks
! ensure you can run `$ bundle exec rake -P` against your app
! and using the production group of your Gemfile.
! rake aborted!
! NameError: uninitialized constant ApplicationPolicy
/tmp/build_64dbe116b1bd38f520ae98d49690e476/sdotapi-86210153dc2b588aa7b0dc9a60799f5090c76f46/app/policies/user_setting_policy.rb:1:in `<top (required)>'
错误所在的行:
class UserSettingPolicy < ApplicationPolicy
class UserSettingPolicy
这两个策略文件都位于app/policies
中
任何关于可能发生的事情的想法都将不胜感激 终于找到了问题。在上面的代码中,策略文件被一个接一个地加载,因此,Rails必须将其更改为“下”,才能立即加载整个目录
config.autoload_paths += Dir[Rails.root.join('app', 'policies', '*.rb')]
# earlier it was Dir[Rails.root.join('app/policies/*.rb')].each &method(:require)
另一方面,
config/application.rb
中的上述行最终是:
config.autoload_paths += Dir[
Rails.root.join('app', 'policies', '*.rb'),
Rails.root.join('app', 'lib', '*.rb')
]
注意
lib
的变化,这是由于Rails 5中自动加载的变化。有关更多详细信息,请参见此处:检查github存储库中的那一行,您确定在推送到heroku之前没有向git提交其他内容吗?@FabrizioBertoglio刚刚提交了,版本是相同的。Github推送触发Heroku部署,因此使用相同的文件。您认为可能发生了什么?哪里定义了ApplicationPolicy
?看起来rails
找不到它。它是您为UserSettingPolicy
设置的super
类,因此rails需要导入该类的所有方法,但它找不到该类ApplicationPolicy
与Pundit
有关?它位于app/policies/application\u policy.rb
中。奇怪的是,这个错误并没有发生在我的本地邮箱上。你的github配置文件确实有app/policies/application\u policy.rb
?是否按照指南生成应用程序策略?您是否尝试使用heroku restart
重新启动heroku服务器,或者尝试heroku运行rails g pundit:install
是一个错误的解决方案?您的生产文件是什么样子的?
config.autoload_paths += Dir[
Rails.root.join('app', 'policies', '*.rb'),
Rails.root.join('app', 'lib', '*.rb')
]