如何从主机应用程序可用的Rails 3引擎中创建路由?

我有一个Rails 3应用程序,其中包含多个包含其他function的引擎。 每个引擎都是一个单独的服务,客户可以购买访问权限。

但是,我对来自引擎的路径存在问题,这些路由不易用于控制器和视图。

控制器:

class ClassroomsController < ApplicationController .. respond_to :html def index respond_with(@classrooms = @company.classrooms.all) end def new respond_with(@classroom = @company.classrooms.build) end .. end 

app/views/classrooms/new.html.haml

 = form_for @classroom do |f| .. f.submit 

引擎中的config/routes.rb

 MyEngineName::Engine.routes.draw do resources :classrooms end 

app中的config/routes.rb

 Seabed::Application.routes.draw do mount MyEngineName::Engine => '/engine' ... end 

引擎中的lib/my_engine_name.rb

 module MyEngineName class Engine < ::Rails::Engine end end 

试图去/classrooms/new结果

 NoMethodError in Classrooms#new Showing app/views/classrooms/_form.html.haml where line #1 raised: undefined method `hash_for_classrooms_path' for # 

并尝试从任何其他视图调用classrooms_path导致相同的错误。 但是,我可以调用MyEngineName::Engine.routes.url_helpers.classrooms_path并使其正常工作。 我想我可能已经定义了错误的路线,但找不到另一种有效的方法。

尝试使用Passenger(独立模块和Apache模块)和WEBrick(rails服务器)运行应用程序。 使用Git最新的Rails( 7c920631ec3b314cfaa3a60d265de40cba3e8135 )。

将引擎中的config.routes更改为:

 Rails.application.routes.draw do # NOT MyEngineName::Engine.routes.draw resources :classrooms end 

您拥有它的方式,路由仅在MyEngineName::Engine命名空间中可用,而不在其余的主机rails应用程序中。

曾经有一篇博文有更多信息,但不幸的是它已不再可用:

我遇到了同样的问题,并在文档中找到了这个:

由于您现在可以在应用程序的路径中安装引擎,因此您无法直接访问Application中的Engine的url_helpers。 在应用程序的路径中安装引擎时,会创建一个特殊帮助程序以允许您执行此操作。 考虑这样的场景:

 # config/routes.rb MyApplication::Application.routes.draw do mount MyEngine::Engine => "/my_engine", :as => "my_engine" get "/foo" => "foo#index" end 

现在,您可以在应用程序中使用my_engine帮助程序:

 class FooController < ApplicationController def index my_engine.root_url #=> /my_engine/ end end 

对我来说也有帮助

 require 'engine' if defined?(Rails) 

到我的主要gem文件(lib / .rb)。

很好的例子 – https://github.com/mankind/Rails-3-engine-example/blob/master/lib/dummy.rb