Ruby on rails 不要使用Rails引擎名称作为路由前缀

Ruby on rails 不要使用Rails引擎名称作为路由前缀,ruby-on-rails,ruby,ruby-on-rails-3,rails-engines,Ruby On Rails,Ruby,Ruby On Rails 3,Rails Engines,我正在将一个应用程序转换为使用Rails引擎。我的引擎位于engines/web文件夹中。在config/routes.rb中,我像这样装载它: config routes.rb engines web config routes.rb app controllers web application_controller.rb ... lib ... module We

我正在将一个应用程序转换为使用Rails引擎。我的引擎位于
engines/web
文件夹中。在
config/routes.rb
中,我像这样装载它:

config
  routes.rb
engines
  web
    config
      routes.rb
    app
      controllers
        web 
          application_controller.rb
          ...
    lib
      ...
module Web
  class Engine < Rails::Engine
    isolate_namespace Web
  end
end
module Web
  class ApplicationController < ::ActionController::Base
    layout 'web/layouts/application'

    # other code
  end
end
mount Web::Engine=>'/,作为:“Web\u Engine”

文件夹结构如下所示:

config
  routes.rb
engines
  web
    config
      routes.rb
    app
      controllers
        web 
          application_controller.rb
          ...
    lib
      ...
module Web
  class Engine < Rails::Engine
    isolate_namespace Web
  end
end
module Web
  class ApplicationController < ::ActionController::Base
    layout 'web/layouts/application'

    # other code
  end
end
引擎的定义如下:

config
  routes.rb
engines
  web
    config
      routes.rb
    app
      controllers
        web 
          application_controller.rb
          ...
    lib
      ...
module Web
  class Engine < Rails::Engine
    isolate_namespace Web
  end
end
module Web
  class ApplicationController < ::ActionController::Base
    layout 'web/layouts/application'

    # other code
  end
end
模块Web
类引擎
Web引擎内部的应用程序控制器定义如下:

config
  routes.rb
engines
  web
    config
      routes.rb
    app
      controllers
        web 
          application_controller.rb
          ...
    lib
      ...
module Web
  class Engine < Rails::Engine
    isolate_namespace Web
  end
end
module Web
  class ApplicationController < ::ActionController::Base
    layout 'web/layouts/application'

    # other code
  end
end
模块Web
类ApplicationController<::ActionController::Base
布局“web/布局/应用程序”
#其他代码
结束
结束

问题是在
Web::ApplicationController
中,我必须将路由称为
Web\u引擎。我的路由路径
,而不是
我的路由路径
。有没有一种方法可以从web引擎内部访问没有
web\u引擎前缀的路由?

这应该可以做到:

module Web
  class Engine < Rails::Engine
    isolate_namespace Web
  end
end
模块Web
类引擎
有关路线的详细信息,请参见。还可以查看引擎。隔离名称空间
说明。

您必须

1-从您的\u engine\u path\u/lib/web/engine.rb文件中删除行
隔离的\u名称空间
(我假设您知道对象命名冲突风险的后果)

2-从您的_app\u path\u/config/route.rb文件中删除行
mount Web::Engine…

3-在
Rails.application.routes.draw
块中定义引擎中的路由。因此,您的_engine_path_u/config/routes.rb如下所示:

Rails.application.routes.draw do
  resources :things
  get 'my_path', to: 'my_controller#my_action'
end
然后,在引擎中定义的路由将不会以引擎名称作为前缀


您将可以从应用程序中的引擎访问路由帮助程序,反之亦然,无需前缀和引擎名称的范围

在引擎控制器中包含url帮助程序:

module Web
  class ApplicationController < ::ActionController::Base
    helper Web::Enginer.routes.url_helpers

  end
end
模块Web
类ApplicationController<::ActionController::Base
helper Web::Enginer.routes.url\u helpers
结束
结束

查看文档,我无法判断这是否会强制重新构造引擎文件。@NewAlexandria routes部分是在假设您使用独立名称空间的情况下编写的。它包含以下单词:
例如,如果模板是从应用程序呈现的,那么下面的示例将转到应用程序的posts\u路径;如果模板是从引擎呈现的,则转到引擎的posts\u路径:link\u到“Blog posts”,posts\u路径
。这就是我所拥有的。仍然在Web引擎的控制器中,我必须在所有路由前面加上“Web”:
Web。我的路径
@AndrewBezzub它肯定能工作。下面是一些非常好的描述:。我做了一个测试,一切都很顺利。此外,我还必须通过
config.railties\u order=[TestEngine::Engine,:main\u app,:all]
更改railsties的加载顺序(有关名为railties\u order的选项的详细信息,请参阅我的链接文档),以使其能够装载到“/”。看看你的链接,安德鲁不想给他们打电话;作为
my\u engine.root\u url
是否有办法不使用可装载引擎为路由添加前缀?我可以在应用程序中为路由添加前缀,我不希望它们在作为引擎一部分的代码中添加前缀
是否有办法不使用可装载引擎为路由添加前缀