how to override rails 3 engine models and controllers in the main application?

浪尽此生 提交于 2020-01-05 10:20:49

问题


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

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).

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.


回答1:


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::Concern

More details here, http://guides.rubyonrails.org/engines.html#overriding-models-and-controllers

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



来源:https://stackoverflow.com/questions/4009553/how-to-override-rails-3-engine-models-and-controllers-in-the-main-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!