简体   繁体   English

如何使用url参数访问root_path(在主机应用程序中,从Rails引擎)?

[英]How to access root_path with url param (in host app, from rails engine)?

Setup 设定

engine's routes.rb : 引擎的routes.rb

MyEngine::Engine.routes.draw do
  get '/:guid', :to => 'foo_bar#index'
end

mounted in host app via mount MyEngine::Engine, :at => '/some_route' 通过mount MyEngine::Engine, :at => '/some_route'安装在主机应用程序中

Issue 问题

Somewhere in a helper in the host app: 主机应用程序中某个助手中的某个位置:

generated_link = my_engine.root_path(some_guid_i_got)

results in 结果是

undefined method `root_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007ff443d405a0>

I tried adding include MyEngine::Engine.routes.url_helpers , but that broke a bunch of other routes that aren't even related to the engine. 我尝试添加include MyEngine::Engine.routes.url_helpers ,但这破坏了许多其他甚至与引擎无关的路由。 Engine is working other than this url-helper, but I don't want to hard-code the url in. 引擎正在工作,而不是此url-helper,但我不想在其中硬编码URL。

What's the proper way of generating the url for /some_route/a-guid-here ? /some_route/a-guid-here生成url的正确方法是什么?

Thanks in advance 提前致谢

The error is letting you know what's wrong. 错误是让您知道出了什么问题。 undefined method root_path is letting you know that it can't find the root_path defined and that is a required path to have in your routes file. undefined method root_path让您知道它找不到已定义的root_path ,这是您的路由文件中必须具有的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM