Ruby on rails 不要使用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
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
是否有办法不使用可装载引擎为路由添加前缀?我可以在应用程序中为路由添加前缀,我不希望它们在作为引擎一部分的代码中添加前缀是否有办法不使用可装载引擎为路由添加前缀是的,这就是我的答案。只需按照我的答案中的3个步骤操作,您的路线将不再加前缀。这不是一个“可安装”的引擎。好的,我有一个带有5个引擎的项目,我的路线没有前缀。按照我的步骤去做,它会成功的。他向安德鲁保证。请用engines/web/config/routes.rb、config/routes文件和rake/routes输出上的代码更新您的问题,好吗?