Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Print message on stderr when no routes are defined. #9316

Merged
merged 1 commit into from

7 participants

Steve Klabnik José Valim Franklin Webber Xavier Noria Prem Sichanugrist Wojciech Wnętrzak Jordon Bedwell
Steve Klabnik
Collaborator

Newbies get confused when they run 'rake routes' and no routes are defined. Let's
point them to some documentation.

Thanks to @burtlo for the suggestion.

$ 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

routes

Steve Klabnik
Collaborator

Added all the feedback @jeremy gave me.

We don't seem to have any tests on the HTML format, so I didn't write a new one... is it worth it? Also, the linkify helper is a bit... yeah. But given that this is the only place it's currently used, should be fine.

Any other thoughts, or should I :shipit: ?

José Valim
Owner

Wouldn't it make more sense to just call formatter.no_routes and the formatter will generate the error message and show in the most appropriate format? This seems to be doing a lot of work and it would be easier/simpler to generate each message by hand.

Steve Klabnik
Collaborator

@josevalim yeah, especially since this is only happening in two places. I dig it. Changing!

Steve Klabnik
Collaborator

Much cleaner, @josevalim. Good call. That's what happens when you start one place and end up another...

actionpack/lib/action_dispatch/routing/inspector.rb
@@ -90,6 +90,12 @@ def format(formatter, filter = nil)
routes_to_display = filter_routes(filter)
routes = collect_routes(routes_to_display)
+
+ unless routes.any?
Wojciech Wnętrzak
morgoth added a note

if routes.none? looks simpler for me.

Steve Klabnik Collaborator

I always forget about none. I agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Franklin Webber

:+1: I approve this message.

Steve Klabnik steveklabnik 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.
d3b836a
Steve Klabnik
Collaborator

@fxn fixed per your comments.

Xavier Noria
Owner

Looks good to me, the space before the period hurts my eyes but it is easier that I edit later, good to go!

Xavier Noria fxn merged commit 33310fb into from
Prem Sichanugrist sikachu commented on the diff
actionpack/lib/action_dispatch/routing/inspector.rb
@@ -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
+
Prem Sichanugrist Collaborator
sikachu added a note

Y U NO USE #strip_heredoc :cry:

Steve Klabnik Collaborator

I don't know what that is.

[2] pry(main)> def hello_world
[2] pry(main)*   <<-STR.strip_heredoc
[2] pry(main)*     win
[2] pry(main)*   STR
[2] pry(main)* end  
=> nil
[3] pry(main)> hello_world
=> "win\n"

@steveklabnik

Prem Sichanugrist Collaborator
sikachu added a note

FTR this was fixed in 13c9195

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 19, 2013
  1. Steve Klabnik

    Add message when you have no routes defined.

    steveklabnik authored
    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.
This page is out of date. Refresh to see the latest.
6 actionpack/CHANGELOG.md
View
@@ -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"`
25 actionpack/lib/action_dispatch/routing/inspector.rb
View
@@ -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
+
Prem Sichanugrist Collaborator
sikachu added a note

Y U NO USE #strip_heredoc :cry:

Steve Klabnik Collaborator

I don't know what that is.

[2] pry(main)> def hello_world
[2] pry(main)*   <<-STR.strip_heredoc
[2] pry(main)*     win
[2] pry(main)*   STR
[2] pry(main)* end  
=> nil
[3] pry(main)> hello_world
=> "win\n"

@steveklabnik

Prem Sichanugrist Collaborator
sikachu added a note

FTR this was fixed in 13c9195

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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")
15 railties/test/application/rake_test.rb
View
@@ -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
Something went wrong with that request. Please try again.