在 Rails 中,当我在运行时通过路由在插件之间切换时?
在 Rails 中,当我在运行时通过路由在插件之间切换(启用/禁用/加载/卸载)时?
为了加载我在文件“config/application.rb”中使用的特定插件:
module MyApp
class Application < Rails::Application
#when starting the server
#the application only loads the plugin "default_app"
config.plugins = [:default_app]
end
end
但我需要在运行时加载其他插件,例如插件“:x”和“:y” 并能够在插件之间切换,如下所示:
#execute before every request
ActionDispatch::Callbacks.before do
#making a request to: localhost:3000/load_plugin/x/:controller/:action
#should load:
MyApp.Application.config.plugins = [:default_app,:x]
#making a request to: localhost:3000/load_plugin/y/:controller/:action
#should load:
MyApp.Application.config.plugins = [:default_app,:y]
end
知道我应该怎么做吗?
编辑
我无法在启动时执行此操作,因为插件必须具有以下结构:
- vendor/plugins/x/app/controllers/a_controller.rbvendor
- /plugins/y/app/controllers/a_controller.rb
- (许多其他)
每个文件必须根据要求替换另一个文件。 对于模型和视图以及其他文件(例如资产文件夹)来说,这也应该是正确的
In Rails as I switch (enable / disable / loading / unloading) between plugins through routes at runtime?
For loading a especific plugin i use in the file "config/application.rb":
module MyApp
class Application < Rails::Application
#when starting the server
#the application only loads the plugin "default_app"
config.plugins = [:default_app]
end
end
But I need to load other plugins at runtime, for example, a plugin ":x" and a ":y"
and be able to switch between plugins as follows:
#execute before every request
ActionDispatch::Callbacks.before do
#making a request to: localhost:3000/load_plugin/x/:controller/:action
#should load:
MyApp.Application.config.plugins = [:default_app,:x]
#making a request to: localhost:3000/load_plugin/y/:controller/:action
#should load:
MyApp.Application.config.plugins = [:default_app,:y]
end
Any idea how I should do this?
EDIT
I cannot do this in startup, because the plugins must have the following structure:
- vendor/plugins/x/app/controllers/a_controller.rb
- vendor/plugins/y/app/controllers/a_controller.rb
- (many others)
each file must replace the other in accordance with the request.
this should be true too, for models and views and anothers files like the assets folder
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是一个很好的答案,但是您是否尝试过使用
ActiveSupport::Dependency.autoload_once_paths
?这个变量告诉 Rails 要重新加载哪些文件。正如预期的那样,它的目的是用于开发(更改文件并查看更改,而无需重新启动服务器),而且,据我所知,它并不意味着在生产中使用(在每个请求上重新加载代码似乎是一脚好针) 。然而,它似乎正是您正在寻找的东西。
Not a good answer, but have you tried mucking around with
ActiveSupport::Dependencies.autoload_once_paths
?This is the variable that tells Rails what files to reload. As expected, its purpose is for development (change a file and see the change without having to restart the server) and, AFAIK, it's not meant to be used in production (reloading code on every request seems like a good shot in the foot). However, it seems to be exactly what you're looking for.