安装导轨引擎中的命名路线

发布于 2024-12-14 00:08:43 字数 808 浏览 0 评论 0原文

我正在制作一个小型 Rails 引擎,如下安装:

mount BasicApp::Engine => "/app"

使用 这个答案 我已经验证了引擎中的所有路由都应该是这样:

但是 - 当我(在引擎内部)链接到命名路由(在引擎内部定义)时,我收到此错误

undefined local variable or method `new_post_path' for #<#<Class:0x000000065e0c08>:0x000000065d71d0>

正在运行“耙路”清晰验证“new_post”应该是一个命名路径,所以我不知道为什么Rails(3.1.0)无法弄清楚它。欢迎任何帮助,

我的 config/route.rb (对于引擎)看起来像这样,

BasicApp::Engine.routes.draw do
  resources :posts, :path => '' do
                resources :post_comments
                resources :post_images
        end
end

我应该补充一点,它是独立的引擎。然而像 main_app.root_path 这样的路径工作正常 - 而 root_path 则不行

I'm making a small rails engine which I mount like this:

mount BasicApp::Engine => "/app"

Using this answer I have verified that all the routes in the engine are as the should be:

However - when I (inside the engine) link to a named route (defined inside the engine) I get this error

undefined local variable or method `new_post_path' for #<#<Class:0x000000065e0c08>:0x000000065d71d0>

Running "rake route" clearly verifies that "new_post" should be a named path, so I have no idea why Rails (3.1.0) can't figure it out. Any help is welcome

my config/route.rb (for the engine) look like this

BasicApp::Engine.routes.draw do
  resources :posts, :path => '' do
                resources :post_comments
                resources :post_images
        end
end

I should add that it is and isolated engine. However paths like main_app.root_path works fine - while root_path does not

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彩扇题诗 2024-12-21 00:08:43

我认为最好的解决方案的正确方法

是在引擎的路由代理上调用 new_post_path ,该代理可作为辅助方法使用。在您的情况下,帮助程序方法将默认为 basic_app_engine,因此您可以在视图或帮助程序中调用 basic_app_engine.new_post_path

如果需要,您可以通过两种方式之一设置名称。

# in engine/lib/basic_app/engine.rb:
module BasicApp
  class Engine < ::Rails::Engine
    engine_name 'basic'
  end
end

或者

# in app/config/routes.rb:
mount BasicApp::Engine => '/app', :as => 'basic'

无论哪种情况,您都可以在视图或助手中调用 basic.new_posts_path

一种选择是不使用已安装的引擎,而是让引擎将路由直接添加到应用程序。 Thoughtbot 的 HighVoltage 可以实现此目的。我不喜欢这个解决方案,因为当您添加许多引擎时它可能会导致命名空间冲突,但它确实有效。

# in engine/config/routes.rb
Rails.application.routes.draw do
  resources :posts, :path => '' do
                resources :post_comments
                resources :post_images
  end
end

# in app/config/routes.rb:
# (no mention of the engine)

The right way

I believe the best solution is to call new_post_path on the Engine's routes proxy, which is available as a helper method. In your case, the helper method will default to basic_app_engine, so you can call basic_app_engine.new_post_path in your views or helpers.

If you want, you can set the name in one of two ways.

# in engine/lib/basic_app/engine.rb:
module BasicApp
  class Engine < ::Rails::Engine
    engine_name 'basic'
  end
end

or

# in app/config/routes.rb:
mount BasicApp::Engine => '/app', :as => 'basic'

In either case, you could then call basic.new_posts_path in your views or helpers.

Another way

Another option is to not use a mounted engine and instead have the engine add the routes directly to the app. Thoughtbot's HighVoltage does this. I don't love this solution because it is likely to cause namespace conflicts when you add many engines, but it does work.

# in engine/config/routes.rb
Rails.application.routes.draw do
  resources :posts, :path => '' do
                resources :post_comments
                resources :post_images
  end
end

# in app/config/routes.rb:
# (no mention of the engine)
べ繥欢鉨o。 2024-12-21 00:08:43

在 Rails 4 上,engine_name 指令对我不起作用。
要从引擎自己的视图或控制器访问引擎路由中定义的命名路由,我最终使用了详细的方法,

BasicApp::Engine.routes.url_helpers.new_post_path

我建议定义一个简单的辅助方法以使其更可用

# in /helpers/basic_app/application_helper.rb

module BasicApp::ApplicationHelper
  def basic_app_engine
    @@basic_app_engine_url_helpers ||= BasicApp::Engine.routes.url_helpers
  end
end

有了这个,您现在可以使用

basic_app_engine.new_post_path

如果您需要访问您的主引擎中的应用程序助手,您只需使用 main_app

main_app.root_path

On Rails 4 the engine_name directive did not work for me.
To access a named route defined in engine's routes from engine's own view or controller, I ended up using the verbose

BasicApp::Engine.routes.url_helpers.new_post_path

I recommend defining a simple helper method to make this more usable

# in /helpers/basic_app/application_helper.rb

module BasicApp::ApplicationHelper
  def basic_app_engine
    @@basic_app_engine_url_helpers ||= BasicApp::Engine.routes.url_helpers
  end
end

With this in place you can now use

basic_app_engine.new_post_path

In case you need to access your main application helper from the engine you can just use main_app:

main_app.root_path
海螺姑娘 2024-12-21 00:08:43

在您的应用程序中使用以下内容来访问引擎路线

MyApp::Engine.routes.url_helpers.new_post_path

use the below in you app to access the engine routes

MyApp::Engine.routes.url_helpers.new_post_path
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文