我实际上在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 %>
但它对我帮助不大。
尝试:
<li><%= link_to "Blog Posts", Rails.application.routes.url_helpers.root_path %></li>
在您的routes.rb文件中假设您有以下内容:
root "home#index"
您可以通过在引擎的url_helpers
中添加主应用程序的ApplicationController
来实现此目标。
module YourEngine
class ApplicationController < ::ApplicationController
helper Rails.application.routes.url_helpers
end
end
应该这样做!
尝试:
<li><%= link_to "Blog Posts", main_app.root_path %></li>
main_app
允许您访问宿主应用程序的所有url帮助程序。
它在https://guides.rubyonrails.org/engines.html#routes和https://api.rubyonrails.org/classes/Rails/Engine.html的Rails文档中有所描述