【问题标题】:Listing 'rake routes' for a mountable Rails 3.1 engine列出可安装 Rails 3.1 引擎的“rake routes”
【发布时间】:2011-11-17 21:40:12
【问题描述】:

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

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

$ rails plugin new rails_blog_engine --mountable

并编辑了“test/dummy/config/routes”文件以读取:

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

...和“配置/路由”阅读:

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 工作正常。一个简单的rake routes 确实会引发相同的错误,但这是意料之中的。
  • 今天写引擎,遇到了这个问题,才发现3年前也有同样的问题! (参考我上面的评论)有些事情永远不会改变,嗯? :)

标签: ruby-on-rails routes ruby-on-rails-3.1 rails-routing rails-engines


【解决方案1】:

如果人们在 cmets 中遗漏了它,截至Rails 3.2.2,您现在可以使用

$ rake app:routes

【讨论】:

    【解决方案2】:

    如果您将标准 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 及更高版本中,您需要一个 newer version of the rake routes 任务。

    【讨论】:

    • 谢谢!为我工作。这似乎很奇怪,因为它不会被包含在盒子之外。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      用于导轨 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
      

      导轨 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"
      

      【讨论】:

      • 谢谢...从这里我能够弄清楚如何从引擎访问路由 url_helpers,例如"IssueTracker::Engine".constantize.routes.url_helpers,我可以通过它动态send 到我需要的路径助手.
      【解决方案5】:

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

      bundle exec rake app:routes
      

      【讨论】:

        【解决方案6】:

        在导轨中 3.2X 如果你在你的“host_app”中并且已经安装了一个引擎,你可能可以通过执行来列出路由(应该开箱即用):

        bundle exec rake engine_name:routes
        

        【讨论】:

          猜你喜欢
          • 2011-11-19
          • 2011-08-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多