【问题标题】:How to access root_path with url param (in host app, from rails engine)?如何使用 url 参数访问 root_path(在主机应用程序中,来自 rails 引擎)?
【发布时间】:2019-02-22 02:08:48
【问题描述】:

设置

引擎的routes.rb:

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

通过mount MyEngine::Engine, :at => '/some_route'安装在主机应用中

问题

宿主应用程序中的某个帮助器:

generated_link = my_engine.root_path(some_guid_i_got)

结果

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

我尝试添加include MyEngine::Engine.routes.url_helpers,但这破坏了许多甚至与引擎无关的其他路线。 引擎在这个 url-helper 之外工作,但我不想硬编码 url。

/some_route/a-guid-here生成网址的正确方法是什么?

提前致谢

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

    错误是让您知道出了什么问题。 undefined method root_path 让您知道它找不到定义的 root_path,这是您的路由文件中必需的路径。

    【讨论】:

    • 嗨,谢谢。您的意思是/ 是必填路线吗?即使是引擎?以及如何为/:guid 生成路由?
    猜你喜欢
    • 2011-05-20
    • 2012-06-23
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多