Browse files

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.
  • Loading branch information...
1 parent 8fd17e0 commit d3b836ac4a35c1599be1fea0b72d5f19a9a37d4e @steveklabnik committed Feb 18, 2013
View
6 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"`
View
25 actionpack/lib/action_dispatch/routing/inspector.rb
@@ -91,6 +91,11 @@ def format(formatter, filter = nil)
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 @@ def header(routes)
@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 @@ def section(routes)
@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")
View
15 railties/test/application/rake_test.rb
@@ -143,6 +143,21 @@ def test_rake_routes_calls_the_route_inspector
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

0 comments on commit d3b836a

Please sign in to comment.