Rails 在 lib 文件夹中找不到文件
Rails not finding files on lib folder
每次控制器或模型尝试访问 /lib 文件夹上的 class 时,它会显示:
NameError (uninitialized constant 'current_controller':'class_name' did you mean 'something_else')
是的,我知道 rails 命名约定并且我正在正确使用它。我在其他几台服务器(Ubuntu & CentOS 6)中有代码 运行。它仅在我们拥有的这 2 台 RedHat7.2 服务器上出错 - 在所有服务器上都完全相同 ruby/rails/gems。我尝试使用的任何库文件都会出现错误。 SELinux 被禁用。
Ruby 版本 2.3.3; Rails 版本 5.1.0(在所有服务器上相同)
有人有什么想法吗? Rails 假设自动加载那些 class 文件。
在 rails < 5:
config/application.rb
module your_app
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
config.autoload_paths += %W(#{config.root}/lib/path)
end
end
在 rails >= 5
module your_app
class Application < Rails::Application
config.eager_load_paths << "#{Rails.root}/lib/path"
end
end
如果您希望 rails 自动加载您的库,您必须将它放在 app
文件夹下。
每次控制器或模型尝试访问 /lib 文件夹上的 class 时,它会显示:
NameError (uninitialized constant 'current_controller':'class_name' did you mean 'something_else')
是的,我知道 rails 命名约定并且我正在正确使用它。我在其他几台服务器(Ubuntu & CentOS 6)中有代码 运行。它仅在我们拥有的这 2 台 RedHat7.2 服务器上出错 - 在所有服务器上都完全相同 ruby/rails/gems。我尝试使用的任何库文件都会出现错误。 SELinux 被禁用。
Ruby 版本 2.3.3; Rails 版本 5.1.0(在所有服务器上相同)
有人有什么想法吗? Rails 假设自动加载那些 class 文件。
在 rails < 5:
config/application.rb
module your_app
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
config.autoload_paths += %W(#{config.root}/lib/path)
end
end
在 rails >= 5
module your_app
class Application < Rails::Application
config.eager_load_paths << "#{Rails.root}/lib/path"
end
end
如果您希望 rails 自动加载您的库,您必须将它放在 app
文件夹下。