【问题标题】:Rails 4 Engine not loading the Routes from Root appRails 4 Engine 未从 Root 应用程序加载路由
【发布时间】:2016-05-17 09:39:44
【问题描述】:

我创建了一个新的 rails 4 引擎并尝试为新创建的引擎不使用任何挂载路线,但它对我不起作用,下面是文件。

app/routes.rb(根路由文件)

Rails.application.routes.draw do
  mount Uhoh::Engine => "/uhoh"
  resources :products
end

new_engine/config/routes.rb(引擎路由文件)

Uhoh::Engine.routes.draw do
  get "failures#index"
end

uhoh/lib/uhoh/engine.rb(引擎文件)

module Uhoh
  class Engine < ::Rails::Engine
    isolate_namespace Uhoh
  end
end

但是当我从 treminal 运行“rake routes”命令时,它不会显示来自“Uhoh”引擎的路由。

Prefix Verb   URI Pattern                  Controller#Action
        uhoh        /uhoh                        Uhoh::Engine
    products GET    /products(.:format)          products#index
             POST   /products(.:format)          products#create
 new_product GET    /products/new(.:format)      products#new
edit_product GET    /products/:id/edit(.:format) products#edit
     product GET    /products/:id(.:format)      products#show
             PATCH  /products/:id(.:format)      products#update
             PUT    /products/:id(.:format)      products#update
             DELETE /products/:id(.:format)      products#destroy

Routes for Uhoh::Engine:

【问题讨论】:

  • 你的Uhoh路由文件在uhoh/config/routes.rb吗?
  • 是的,它在 uhoh/config/routes.rb 中

标签: ruby-on-rails ruby ruby-on-rails-4 rails-engines


【解决方案1】:

$ rails plugin new blorgh --mountable 应用程序目录树 一个 config/routes.rb 文件: lib/blorgh/engine.rb 中的一个文件,其功能与标准 Rails 应用程序的 config/application.rb 文件相同: 模块 Blorgh 类引擎

--mountable 选项将添加到--full 选项:

资产清单文件(application.js 和 application.css) 命名空间的 ApplicationController 存根 命名空间的 ApplicationHelper 存根 引擎的布局视图模板 命名空间隔离到 config/routes.rb:

Blorgh::Engine.routes.draw 做 结束

与 lib/blorgh/engine.rb 的命名空间隔离:

模块 Blorgh 类引擎

此外,--mountable 选项告诉生成器通过将以下内容添加到位于 test/dummy/config/routes.rb 的虚拟应用程序的路由文件中,将引擎安装在位于 test/dummy 的虚拟测试应用程序中:

mount Blorgh::Engine => "/blorgh"

app/controllers/blorgh/articles_controller.rb:

require_dependency "blorgh/application_controller"

模块 Blorgh 类 ArticlesController

【讨论】:

  • 我遵循了相同的文档,但为什么我的路由在我的根应用程序文件夹中不起作用??
猜你喜欢
  • 2014-08-03
  • 1970-01-01
  • 2012-04-19
  • 2017-08-08
  • 2015-08-11
  • 1970-01-01
  • 2011-09-24
  • 1970-01-01
  • 2018-01-30
相关资源
最近更新 更多