English 中文(简体)
申请主计长的发动机路线
原标题:Engine routes in Application Controller

我有过之前的“过滤器 h在我的主要应用控制器中,这种控制器具有类似之处:(它只是在闪电中设置了链接,但信息与问题无关,它只是从该方法的路线上进入)

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,否则这些路线的助动器是用于发动机的。

So changing the method in the application controller to

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

排除了例外情况,但我确实不想这样做。 是否有另一种办法使发动机认识到主要——应用路线?

EDIT:

如果申请的布局需要路人帮助,也会出现这种情况。 因此,如果该发动机的设计是为了融入主要——应用布局,那么这一问题也会随之而来。

最佳回答

可用发动机的设计就是要像这样的工作,即连接主要补给线和发动机路线。

如果你想要合并这两套路线,你就可以使用非同位素发动机。 第一步是删除<条码>同位素-名称。 2. 发动机定义中所用的方法:

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

第二步是把你在<条码>上的路线改为:

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

为此:

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

www.un.org/Depts/DGACM/index_french.htm 2. 您的申请路线上所用的方法:

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

这样做的主要好处是:

  1. 无需mon铁。 我知道这样做,但从火车上有发动机的时代起,这可能是遗留下来的。

  2. 无需在应用路线上安装发动机。 另一方面,如果你想更精确地控制插入点,因为你的所有发动机路线都将在你的主要路线(或之前,我没有回答这个问题)之后被点击,这可能会回击。

如果您重新寻找关于发动机的文件,那么,为发动机舱提供的铁路是相当好的起点。 我强烈建议,如果你对这个主题重新感兴趣的话,你会读一下(如果是你的话)。

问题回答

我描述了如何做到这一点。 问题属于孤立的名称空间。 为了将发动机与仪器结合起来,并分享同样的布局(可能有主机的助路器) 我这样做了:

首先,我删除了<代码>config/routes.rb。 A. 来自发动机

然后,我从发动机类别中删除了单号——名称空间。

module MyEngine
   class Engine < Rails::Engine
-    isolate_namespace MyEngine
   end
 end
end

我增加了一个在发动机上装载的文件:

module ActionDispatch::Routing
  class Mapper
    def mount_my_engine_at(mount_location)
      scope mount_location do
        #Declare all your routes here
      end
    end
  end
end

最后,在主注<代码>config/routes.rb中,你可以称之为您的方法,而不是装上发动机。

mount_my_engine_at "mount_location"

这基本上将提升你的发动机,作为主机的一部分,而不是孤立。 这与如何做到这一点类似。

You can keep the isolate_namespace. In your engine routes.rb

MyEngine::Engine.routes.draw do
  ...
  root to: "something#index"
end

Rails.application.routes.draw do
  get "something", to: "my_engine/something#index"
end

然后,在主要航道上。

Rails.application.routes.draw do

  mount MyEngine::Engine => "/anything_you_want"

  root to: "main#index"
end

这样,你就可以选择你想要暴露的路线(以及你不暴露的路线)。

如铁路发动机指南所强烈建议,你可以保留地名称:

# inside your main_app s config/routes.rb
Rails.application.routes.draw do
  root to:  home#index 

  mount MyEngine::Engine, at: "/" 
end
# inside your engine s controller
module MyEngine
  class SomeController << ::ApplicationController
    # include main_app s route helpers
    helper Rails.application.routes.url_helpers

  end
end

在你看来,确保所有助手都采用正确的路线代理方法(如我_engine.pages_path)。

你们的主要——应用布局和发动机控制器将正确与主要仪器连接。 你们不必在主食的任何地方添加“主要——适用”的序号。 唯一的 down路是,你将发动机的路线重新铺在主干道上,这条路可以同任何路线相撞。 如果你去做非异构体——名称空间,预计会有任何进展。

The easiest way is to draw the routes in both the main app, and the engine, so that they are accessible to both:

[MyEngine::Engine, App::Application].each do |app|
  app.routes.draw do
    # Declare all your routes here
  end
end




相关问题
Access two models in one view in Rails

Background: I m trying to create a simple feed reader for which I m using the feedzirra. I am developing in Rails 3. This is probably pretty simple, but I m trying to figure out how to access the ...

Rails Faking a Route

To be specific, I m trying to get ActionController::Routing::Routes.recognize_path to recognize a route that is not in routes.rb, for testing purposes. Is it possible to somehow mock or dynamically ...

Rails: model_url for custom action

Let s say I have the simple rails blog app. And I have a custom action, like page_views which shows the number of views of the post. class PostsController < ApplicationController def ...

How can I redirect requests from Rails to a PHP subdomain?

I have a site, say, example.com, which is PHP. I am converting it to Rails, but there are three years worth of issues (like magazine issues) that I don t want to update. Thankfully, it seems that I ...

Complex Rails Routing

I would like to do something like github has with nested urls, and like How do I route user profile URLs to skip the controller? but not really sure how to go on with it. For example, looking at a ...

Ruby on Rails: Can I route a request directly to a view?

I have an admin section that has a sub-directory of the controllers directory. I.e., the directory app/controllers/admin/ holds a set of files, each containing a controller for handling a separate ...

热门标签