应用程序控制器中的引擎路由 [英] Engine routes in Application Controller

查看:46
本文介绍了应用程序控制器中的引擎路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主应用程序的应用程序控制器中有一个 before_filter 钩子,它执行以下操作:(它不只是在 flash 中放置一个链接,还有一条消息,但它与问题无关,它只是访问方法中的路由)

I have a before_filter hook in my main app's application controller that does something like: (It doesn't just put a link in the flash, there is a message, but it isn't relevant to the question, it just accesses the route in the method)

class ApplicationController < ActionController::Base
  before_filter :set_link

  def set_link
    flash[:notice] = items_path
  end
end

这适用于应用程序,但是当我进入我制作的引擎的控制器时,我得到了异常

This works fine for the app, however when I go into the controllers for an engine I made I get the exception

No route matches {:controller=>"items", :action=>"index"}

我知道在引擎中时,路由助手是针对引擎的,除非前缀为 main_app

I understand that when in the engine, the routes helpers are for the engine unless prefixed with main_app

因此将应用程序控制器中的方法更改为

So changing the method in the application controller to

  def set_link
    flash[:notice] = main_app.items_path
  end

摆脱了异常,但我真的不想这样做.是否有另一种让引擎识别 main_app 路由的解决方案?

Gets rid of the exception but I really don't want to have to do that. Is there another solution to getting the engine to recognize the main_app routes?

如果应用程序布局调用路径助手,也会发生这种情况.因此,如果引擎被设计为集成到 main_app 的布局中,那么这个问题也会出现.

This also happens if the application layout calls path helpers. So if the engine is designed to integrated into the main_app's layout then this issue will crop there up too.

推荐答案

可挂载的引擎被设计成这样工作,即隔离主应用程序路由和引擎路由.

Mountable engines are designed to work like this, that is isolate the main app routes and the engine routes.

如果要合并两组路由,可以使用非隔离引擎.第一步是删除引擎定义中的 isolated_namespace 方法调用:

If you want the two sets of routes to be merged, you can use a non-isolated engine. The first step is removing the isolated_namespace method call in your engine definition:

module MyEngine
  class Engine < Rails::Engine
    isolate_namespace MyEngine # remove this line
  end
end

第二步是在my_engine/config/routes.rb中转换你的路由,你应该从这个开始:

The second step is to convert your routes in my_engine/config/routes.rb, you should go from this:

MyEngine::Engine.routes.draw do
  # stuff that routes things
end

到此:

Rails.application.routes.draw do
  # stuff that routes things
end

并删除应用程序路由中的 mount 方法调用:

and remove the mount method call in your application's routes:

App::Application.routes.draw do
  mount MyEngine::Engine => "/engine" # remove this line
end

这样做的主要优点是:

  1. 无需修补导轨.我知道设计会这样做,但这可能是轨道中不存在引擎的时代遗留下来的.

  1. No need to monkey-patch rails. I know devise does this, but this could be a leftover from the days when engines didn't exist in rails.

无需在应用程序路由中安装引擎.另一方面,如果您想更精确地控制插入点,这可能会适得其反,因为您的所有引擎路线都将在您的主要路线之后(或之前,我没有这个问题的答案)被调用.

No need to mount the engine in the application routes. On the other hand, this could backfire if you'd like to control more precisely the insertion point as all you engine routes would be called after (or before, I don't have the answer to this question) your main routes.

如果您正在寻找关于引擎的文档,Engine 类的 rails 文档 是一个很好的起点.如果您对该主题感兴趣,我强烈建议您阅读它们(以防您尚未阅读).

If you're looking for documentation on engines, the rails docs for the Engine class are a pretty good starting point. I'd strongly recommend that you read them (in case you haven't yet) if you're interested in the subject.

这篇关于应用程序控制器中的引擎路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆