ruby-on-rails – 在主应用程序中扩展Rails 3引擎的控制器

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在主应用程序中扩展Rails 3引擎的控制器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的应用程序中使用Rails引擎作为宝石.引擎具有多个方法的PostsController,我想在我的主应用程序中扩展控制器逻辑,例如添加一些方法.如果我在主应用程序中创建了PostsController,那么引擎的控制器没有加载.

基于更改ActiveSupport :: Dependencies#require_or_load,有一个问题Rails engines extending functionality提出的解决方

这是唯一/正确的方法吗?如果是的话,我该把代码放在哪里?

EDIT1:

这是Rails 2.x的代码suggested by Andrius

module ActiveSupport::Dependencies
  alias_method :require_or_load_without_multiple,:require_or_load
  def require_or_load(file_name,const_path = nil)
    if file_name.starts_with?(RAILS_ROOT + '/app')
      relative_name = file_name.gsub(RAILS_ROOT,'')
      @engine_paths ||= Rails::Initializer.new(Rails.configuration).plugin_loader.engines.collect {|plugin| plugin.directory }
      @engine_paths.each do |path|
        engine_file = File.join(path,relative_name)
        require_or_load_without_multiple(engine_file,const_path) if File.file?(engine_file)
      end
    end
    require_or_load_without_multiple(file_name,const_path)
  end
end

解决方法

为什么不继承您的应用程序中的引擎控制器类(并将您的路由指向新的子控制器)?声音在概念上类似于扩展内置Devise控制器的方式.
原文链接:https://www.f2er.com/ruby/273658.html

猜你在找的Ruby相关文章