我正在转换一个应用程序以使用 Rails 引擎。我在 engines/web
文件夹中有一个引擎。在 config/routes.rb
中,我这样挂载它:
mount Web::Engine => '/', as: 'web_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
我在 Web 引擎中的应用程序 Controller 是这样定义的:
module Web
class ApplicationController < ::ActionController::Base
layout 'web/layouts/application'
# other code
end
end
问题是在 Web::ApplicationController
内部,我必须将路由称为 web_engine.my_route_path
而不是 my_route_path
。有没有办法从 Web 引擎内部访问没有 web_engine
前缀的路由?
最佳答案
这应该可以解决问题:
module Web
class Engine < Rails::Engine
isolate_namespace Web
end
end
参见 docs有关路线的详细信息。另请查看 Engine.isolate_namespace
说明。
关于ruby-on-rails - 不要在路由前加上 Rails 引擎名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24688628/