从 Rails Engine 覆盖控制器时是否需要原始文件?

发布于 2024-08-12 18:19:27 字数 320 浏览 4 评论 0原文

我正在尝试覆盖 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以笙箫默 2024-08-19 18:19:27

这完全是猜测......

似乎更多的是加载时间问题。就像这样,您的文件在插件之前加载。你的行动位于哪里?配置/初始化?库?

我不确定 Rails Engines 何时加载,因此请尝试一下该位置(应该通过将其放入 lib 中来工作)。

或者,更好的是,通过更改创建您自己的插件,并确保它在原始插件之后加载。

你可能想要更多类似的东西:

SomeController.class_eval do

  def index
    ...
  end

end

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:

SomeController.class_eval do

  def index
    ...
  end

end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文