20

我正在使用Rails 3.1使用的可安裝引擎,並且我想列出引擎的路由。爲可安裝的Rails 3.1引擎列出'rake routes'

我創建利用發動機:

$ rails plugin new rails_blog_engine --mountable 

和編輯 '測試/虛擬/配置/路由' 文件讀取:

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

...和 ​​'配置/路線' 來閱讀:

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

我想列出生成的路由「:帖子」,但我怎麼能做到這一點還不清楚。當我運行

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

當我運行「耙路線」,我得到一個錯誤:「耙應用:路線」,我只得到了「/博客」路線

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

我怎麼能請參閱':posts'的路線?我可以在不重寫相關佣金任務的情況下執行此操作嗎?

+6

現在,在Rails的3.2.2,'耙應用:routes'正常工作。一個簡單的'rake routes'確實會拋出同樣的錯誤,但這是可以預料的。 – Zabba

+0

今天寫了一個引擎,我遇到了這個問題,並且我注意到3年前我有同樣的問題! (參考上面的評論)有些東西永遠不會改變,呃? :) – Zabba

回答

11

如果從標準的Rails 3.1.0 rake routes任務複製代碼到您的Rake文件,並更改頂部閱讀:

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任務。

+1

謝謝!爲我工作。看起來很奇怪,這不會被包括在內。 –

20

如果人失蹤它的意見,作爲Rails 3.2.2,你現在可以使用

$ rake app:routes 
3

鋼軌3.x的引擎,rake routes下發動機的根不工作(這就是爲什麼它需要一些黑客通過複製耙文件)。然而rake routes在測試/ dummy下工作(或者如果使用rspec,則爲spec/dummy)。它會列出所有安裝在發動機和其他發動機上的引擎。

1

爲Rails 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" 
+0

感謝...從這我能弄清楚如何訪問引擎的路由url_helpers,例如''IssueTracker :: Engine「.constantize.routes.url_helpers',我可以動態'發送'到我需要的路徑幫手。 – erroric

-1

在軌3.2X 如果你在你 「host_app」,並有你可以通過執行(應該可以開箱即可)列出路線:

bundle exec rake engine_name:routes 
1

Rails中5,我使用下面的命令可以得到發動機的路線:

bundle exec rake app:routes