繁体   English   中英

为可安装的Rails 3.1引擎列出'rake routes'

[英]Listing 'rake routes' for a mountable Rails 3.1 engine

我正在开发一个可安装的引擎,用于Rails 3.1,我想列出引擎的路由。

我用以下方法创建了引擎:

$ rails plugin new rails_blog_engine --mountable

编辑'test / dummy / config / routes'文件阅读:

Rails.application.routes.draw do
  mount RailsBlogEngine::Engine => "/blog"
end

...和'config / routes'读取:

RailsBlogEngine::Engine.routes.draw do
  resources :posts
end

我想列出为':posts'生成的路线,但目前尚不清楚我是如何做到这一点的。 当我运行'rake app:routes'时,我只获得“/ blog”路线:

$ rake app:routes
rails_blog_engine    /blog    {:to=>RailsBlogEngine::Engine}

当我运行'rake routes'时,我收到一个错误:

$ rake routes
rake aborted!
Don't know how to build task 'routes'

我怎样才能看到':posts'的路线? 我可以不重写相关的rake任务吗?

如果人们在评论中遗漏了它,从Rails 3.2.2 ,您现在可以使用

$ rake app:routes

如果将代码从标准Rails 3.1.0 rake routes任务复制到Rakefile中,并将顶部更改为:

task :routes => 'app:environment' do
  Rails.application.reload_routes!
  all_routes = RailsBlogEngine::Engine.routes.routes

...用你的引擎名称替换RailsBlogEngine,然后你可以通过运行获得一个基本的路由列表:

rake routes

请注意,在Rails 3.1.1及更高版本中,您需要更新版本的rake routes任务。

对于rails 3.x引擎, rake routes在引擎根目录下不起作用(这就是为什么它需要通过复制rake文件进行一些破解)。 但是rake routes在test / dummy下工作(如果使用rspec则为spec / dummy)。 它将列出所有属于开发中的引擎和安装的其他引擎的路径。

对于铁轨3

 desc 'Print out all defined routes inside engine  match order, with names. Target specific controller with CONTROLLER=x.'
  task engine_routes: :environment do
    Rails.application.reload_routes!
    app = ENV['ENGINE'] || "Rails.application"
    all_routes = app.constantize.routes.routes
    require 'rails/application/route_inspector'
    inspector = Rails::Application::RouteInspector.new
    puts inspector.format(all_routes, ENV['CONTROLLER']).join "\n"
  end

Rails 4

 desc 'Print out all defined routes inside engine match order, with names. Target specific controller with CONTROLLER=x.'

  task engine_routes: :environment do
   app = ENV['ENGINE'] || "Rails.application"
   all_routes = app.constantize.routes.routes
   require 'action_dispatch/routing/inspector'
   inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
   puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, ENV['CONTROLLER'])
  end

然后你就可以打电话了

 $rake engine_routes ENGINE="IssueTracker::Engine"

在Rails 5中,我可以使用以下命令获取引擎的路由:

bundle exec rake app:routes

在rails 3.2X中如果您在“host_app”并安装了引擎,您可以通过执行列出路径(应该开箱即用):

bundle exec rake engine_name:routes

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM