aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Klabnik <steve@steveklabnik.com>2013-02-18 10:26:59 -0800
committerSteve Klabnik <steve@steveklabnik.com>2013-02-18 16:09:37 -0800
commitd3b836ac4a35c1599be1fea0b72d5f19a9a37d4e (patch)
tree9244b360a04ee5aaadb6b96810d8f9f51176af9c
parent8fd17e0c4120f762f87b88d0ccc7c75e2ae2e3ed (diff)
downloadrails-d3b836ac4a35c1599be1fea0b72d5f19a9a37d4e.tar.gz
rails-d3b836ac4a35c1599be1fea0b72d5f19a9a37d4e.tar.bz2
rails-d3b836ac4a35c1599be1fea0b72d5f19a9a37d4e.zip
Add message when you have no routes defined.
Print a message in both `rake routes` and at GET "/rails/info/routes" that lets you know you have no routes defined, as well as linking to the Rails Guide on the topic.
-rw-r--r--actionpack/CHANGELOG.md6
-rw-r--r--actionpack/lib/action_dispatch/routing/inspector.rb25
-rw-r--r--railties/test/application/rake_test.rb15
3 files changed, 46 insertions, 0 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index 6940683c8c..4b1778bc92 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -1,5 +1,11 @@
## Rails 4.0.0 (unreleased) ##
+* Add a message when you have no routes defined to both `rake routes` and
+ GET "/rails/info/routes" that lets you know you have none defined and links
+ to the Rails Guide on the topic.
+
+ *Steve Klabnik*
+
* Change `image_alt` method to replace underscores/hyphens to spaces in filenames.
Previously, underscored filenames became `alt="A_long_file_name_with_underscores"`
diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb
index 82dd05a802..d55c73269c 100644
--- a/actionpack/lib/action_dispatch/routing/inspector.rb
+++ b/actionpack/lib/action_dispatch/routing/inspector.rb
@@ -91,6 +91,11 @@ module ActionDispatch
routes = collect_routes(routes_to_display)
+ if routes.none?
+ formatter.no_routes
+ return formatter.result
+ end
+
formatter.header routes
formatter.section routes
@@ -161,6 +166,16 @@ module ActionDispatch
@buffer << draw_header(routes)
end
+ def no_routes
+ @buffer << <<-MESSAGE
+You don't have any routes defined!
+
+Please add some routes in config/routes.rb.
+
+For more information about routes, see the Rails Guide: http://guides.rubyonrails.org/routing.html .
+MESSAGE
+ end
+
private
def draw_section(routes)
name_width, verb_width, path_width = widths(routes)
@@ -197,6 +212,16 @@ module ActionDispatch
@buffer << @view.render(partial: "routes/route", collection: routes)
end
+ def no_routes
+ @buffer << <<-MESSAGE
+<p>You don't have any routes defined!</p>
+<ul>
+<li>Please add some routes in config/routes.rb.</li>
+<li>For more information about routes, please <a href="http://guides.rubyonrails.org/routing.html">see the Rails Guide</a>.</li>
+</ul>
+MESSAGE
+ end
+
def result
@view.raw @view.render(layout: "routes/table") {
@view.raw @buffer.join("\n")
diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb
index a8275a2e76..a205b289fe 100644
--- a/railties/test/application/rake_test.rb
+++ b/railties/test/application/rake_test.rb
@@ -143,6 +143,21 @@ module ApplicationTests
assert_equal "cart GET /cart(.:format) cart#show\n", Dir.chdir(app_path){ `rake routes` }
end
+ def test_rake_routes_displays_message_when_no_routes_are_defined
+ app_file "config/routes.rb", <<-RUBY
+ AppTemplate::Application.routes.draw do
+ end
+ RUBY
+
+ assert_equal <<-MESSAGE, Dir.chdir(app_path){ `rake routes` }
+You don't have any routes defined!
+
+Please add some routes in config/routes.rb.
+
+For more information about routes, see the Rails Guide: http://guides.rubyonrails.org/routing.html .
+MESSAGE
+ end
+
def test_logger_is_flushed_when_exiting_production_rake_tasks
add_to_config <<-RUBY
rake_tasks do