从 Rails Engine 覆盖控制器时是否需要原始文件?
我正在尝试覆盖 Rails 引擎定义的控制器中的操作。
似乎我需要在重新打开类之前需要原始文件,如下所示:
require File.join(RAILS_ROOT, 'vendor/plugins/myplugin/app/controllers/some_controller')
class SomeController
def index
render :text => 'this is my index'
end
end
这是有道理的,但该要求非常丑陋。是否有某种 Rails 魔法可以让我避免最初的需求?
I'm trying to override an action in a controller defined by a Rails Engine.
It seems like I need to require the original file before reopening the class, like so:
require File.join(RAILS_ROOT, 'vendor/plugins/myplugin/app/controllers/some_controller')
class SomeController
def index
render :text => 'this is my index'
end
end
This makes sense, but that require is pretty ugly. Is there some sort of Rails magic that would allow me to avoid the initial require?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这完全是猜测......
似乎更多的是加载时间问题。就像这样,您的文件在插件之前加载。你的行动位于哪里?配置/初始化?库?
我不确定 Rails Engines 何时加载,因此请尝试一下该位置(应该通过将其放入 lib 中来工作)。
或者,更好的是,通过更改创建您自己的插件,并确保它在原始插件之后加载。
你可能想要更多类似的东西:
This is a complete guess...
Seems more of a load timing problem. As in, your file is getting loaded before the plug-in. Where is your action located? config/initializers? lib?
I'm not to sure when Rails Engines gets loaded so play around with the location (should work by putting it in lib).
Or, better yet, create your own plug-in with the changes and make sure it loads after the original.
And you probably want something more like: