rails:在引擎的lib目录中自动加载文件 [英] rails: autoload files inside engine's lib directory

查看:94
本文介绍了rails:在引擎的lib目录中自动加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个引擎在这个Rails应用程序上工作,该引擎是一种子应用程序,它为我现有的应用程序增加了一些路由.

I am working on this rails application with an engine which is sort of sub application adding some more routes to my existing application.

由于使用了滑轨,因此概念非常强大.

The concept is so powerful, thanks to rails.

但是在开发模式下,要在我的引擎lib目录中自动加载文件更改时,我面临着这个奇怪的挑战.每次我在引擎的app目录(无论是模型还是控制器)中进行更改时,它都可以正常工作,但是没有获取lib目录下任何文件的更改.有办法吗?谢谢你的帮助.

But I am facing this weird challenge to autoload file changes inside my engines lib directory in development mode. Every time I make a change inside app directory of engine be it model or controller , it works flawlessly, but no changes to any files under lib directory get's picked up. Is there a way I can do this ? Thanks for your help.

推荐答案

根据 Rails :: Engine docs ,您可以自动加载类似-

According to Rails::Engine docs you can autoload paths like-

class MyEngine < Rails::Engine
  # Add a load path for this specific Engine
  config.autoload_paths << File.expand_path("../lib/some/path", __FILE__)

  initializer "my_engine.add_middleware" do |app|
    app.middleware.use MyEngine::Middleware
  end
end

如果您不想自动加载,则可以使用require语句直接在类中请求文件-

If you don't want to autoload, you can directly require the file in your class with the require statement-

require 'my_engine/my_object'
class MyModel < AR::Base
  ...
end

这将起作用,因为您的引擎已加载到您的应用中,因此您可以访问其中的库.

This will work because your Engine is already loaded in your app, so you can access libs inside of it.

这篇关于rails:在引擎的lib目录中自动加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆