gpt4 book ai didi

ruby-on-rails - 从 Engine in Rails 3.x 应用程序重新打开模型和 Controller

转载 作者:行者123 更新时间:2023-12-05 08:02:05 27 4
gpt4 key购买 nike

我想通过在 Rails 应用程序中重新打开它们来扩展 Engine 中的模型和 Controller 。问题是应用程序启动时它们没有加载。我知道有一些解决方案,比如 Rails engines extending functionalityhow to override rails 3 engine models and controllers in the main application? ,但我怀疑这是由于 rails 的加载顺序造成的,应该有一些巧妙的解决方案。

然后我遇到了这个解决方案:

config.railties_order = [Blog::Engine, :main_app, :all]

然而,Engine 中的模型和 Controller 被加载,而不是 rails 中的模型和 Controller 。只是想知道以前是否有人做过这项工作?

最佳答案

您可以通过让主 Rails 应用程序 Controller 继承自 Rails 引擎来重新打开 Controller 类。这不需要 config.railties_order 来让 Controller 工作,

#/app/controllers/answer_sheets_controller.rb
require YourCustomEngine::Engine.root.join('app', 'controllers', 'your_custom_engine', 'answer_sheets_controller')

class AnswerSheetsController < YourCustomEngine::AnswerSheetsController

出于某种原因,此策略不适用于模型。

关于ruby-on-rails - 从 Engine in Rails 3.x 应用程序重新打开模型和 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10098658/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com