如何在主应用程序中覆盖 Rails 3 引擎模型和控制器? [英] how to override rails 3 engine models and controllers in the main application?

查看:39
本文介绍了如何在主应用程序中覆盖 Rails 3 引擎模型和控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在基础应用中覆盖 Rails 3 引擎的模型和控制器.

I want to be able to override models and controllers of my rails 3 engine in the base app.

检查 $LOAD_PATH,我在那里找到了引擎的 'app/{models,controllers}',但我不能明确要求引擎的模型或控制器文件: require 'engine_name/model_name' 失败并显示没有这样的文件"(尝试使用命名空间(app/controllers/enginename/*)和普通引擎).

Inspecting $LOAD_PATH, I found engine's 'app/{models,controllers}' there, but I can't explicitly require engine's model or controller file: require 'engine_name/model_name' fails with "no such file" (tried with both namespaced(app/controllers/enginename/*) and plain engine).

那么,在不将它们复制到基础应用程序的情况下,在 Rails 3 中扩展引擎模型/控制器的最佳方法是什么?

So, what's the best way to extend engine's models/controllers in rails 3 without copying them to base app?

基本上,这是一个加载顺序问题.所以,如果我明确要求引擎提供模型,一切都可以,但我希望有更好的方法.

Basically, it's a load order problem. So, if I explicitly require model from engine, everything's ok, but I hope there is a better way.

推荐答案

所以我实际上回去写了文档.答案是使用其中之一打开控制器类和模型类,

So I actually went back and wrote the documentation. The answer is to Open Class the Controller and Model classes using either,

  • Class#eval_class
  • ActiveSupport::关注

这里有更多细节,http://guides.rubyonrails.org/engines.html#overriding-models-和-控制器

(已编辑.从edgeguides"子域更改为guides"子域)

(edited. Changed from "edgeguides" subdomain to "guides" subdomain)

这篇关于如何在主应用程序中覆盖 Rails 3 引擎模型和控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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