从rails引擎生成的页面访问url helpers

问题描述 投票:1回答:3

我实际上在rails布局中有这个:

<li><%= link_to "Blog Posts", root_path %></li>

但当我添加blogit gem时,它开始跟随与blogit关联的网址的错误:

undefined local variable or method `root_path' for #<#<Class:0x007f644864ce00>:0x007f6449e8faf8>

所以我怀疑从那里无法访问网址助手。那么可以做些什么来使它们可以访问?我试过在布局中添加这个:

<% include Rails.application.routes.url_helpers %>

但它对我帮助不大。

ruby-on-rails ruby gem
3个回答
1
投票

尝试:

<li><%= link_to "Blog Posts", Rails.application.routes.url_helpers.root_path %></li>

在您的routes.rb文件中假设您有以下内容:

root "home#index"

0
投票

您可以通过在引擎的url_helpers中添加主应用程序的ApplicationController来实现此目标。

module YourEngine
  class ApplicationController < ::ApplicationController
    helper Rails.application.routes.url_helpers
  end
end

应该这样做!


0
投票

尝试:

  <li><%= link_to "Blog Posts", main_app.root_path %></li>

main_app允许您访问宿主应用程序的所有url帮助程序。

它在https://guides.rubyonrails.org/engines.html#routeshttps://api.rubyonrails.org/classes/Rails/Engine.html的Rails文档中有所描述

© www.soinside.com 2019 - 2024. All rights reserved.