基于 Rails Engine 的插件中命名空间的路由问题

发布于 2024-08-24 18:32:14 字数 1517 浏览 3 评论 0原文

我正在尝试创建一个动态界面。我的模型类存在的地方,并且我的控制器是在启动应用程序时动态创建的。

一切都发生在创建资源的我的路由文件中!

ActionController::Routing::Routes.draw do |map|
  map.namespace :admin do |admin|
    TestAdmin.models.each do |m|
      admin.resources m.to_s.tableize.to_sym
    end
  end
end

然后是我的 BeAdmin 类,它执行以下操作:

module TestAdmin
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def beadmin(options = {})
      namespace_name = "Admin"
      class_name = "#{self.to_s.pluralize.capitalize}Controller"
      klass = namespace_name.constantize.const_set(class_name, Class.new(ApplicationController))
      klass.module_eval do

        def index
          render :text => "test"
        end
      end
    end
  end

  def self.models
    all_models = []
    Dir.chdir(File.join(Rails.root, "app/models")) do
      Dir["**/*.rb"].each do |m|
        class_name = m.sub(/\.rb$/,"").camelize
        klass = class_name.split("::").inject(Object){ |klass,part| klass.const_get(part) }
        all_models << "#{class_name}" if klass < ActiveRecord::Base && !klass.abstract_class?
      end
    end
    all_models
  end
end

当您现在浏览到 /admin/users (从用户模型)时,您会看到“test”。所以效果很好!

但然后我只是对浏览器进行了简单的刷新,被调用的控制器变成了 UsersController#index 而不是 Admin::UsersController#index...他由于某种原因失去了它的命名空间...

也许这里的另一个重要方面是我将所有这些添加为插件和用户 Rails Engines,这样我就可以制作一个可插入的接口...

但到目前为止还没有运气,因为我的路线似乎在某个地方迷路了!

预先感谢您的帮助!

耶勒

I'm trying to create a dynamic interface. Where my model classes exist and my controllers are dynamically created when launching the application.

Everything happens in my routes file where the resources are created!

ActionController::Routing::Routes.draw do |map|
  map.namespace :admin do |admin|
    TestAdmin.models.each do |m|
      admin.resources m.to_s.tableize.to_sym
    end
  end
end

And then there is my BeAdmin class, this does the following:

module TestAdmin
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def beadmin(options = {})
      namespace_name = "Admin"
      class_name = "#{self.to_s.pluralize.capitalize}Controller"
      klass = namespace_name.constantize.const_set(class_name, Class.new(ApplicationController))
      klass.module_eval do

        def index
          render :text => "test"
        end
      end
    end
  end

  def self.models
    all_models = []
    Dir.chdir(File.join(Rails.root, "app/models")) do
      Dir["**/*.rb"].each do |m|
        class_name = m.sub(/\.rb$/,"").camelize
        klass = class_name.split("::").inject(Object){ |klass,part| klass.const_get(part) }
        all_models << "#{class_name}" if klass < ActiveRecord::Base && !klass.abstract_class?
      end
    end
    all_models
  end
end

And when you now browse to the /admin/users (from the User model) then you get to see "test". so it works great!

But then I just do a simple refresh of the browser and The controller that is called becomes UsersController#index instead of Admin::UsersController#index... He loses it's namespace for some reason...

Maybe another important aspect here is that I added all this as a plugin and user Rails Engines so I can make a plug-able interface...

But so far no luck because my routes seem to get lost somewhere!

Thanks in advance for your help!

Jelle

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文