Skip to content

Commit ce13f79

Browse files
committed
Better error message when running rake routes with CONTROLLER arg:
- `CONTROLLER` argument can now be supplied in different ways (Rails::WelcomeController, Rails::Welcome, rails/welcome) - If `CONTROLLER` argument was supplied but it does not exist, will warn the user that this controller does not exist - If `CONTROLLER` argument was supplied and no routes could be found matching this filter, will warn the user that no routes were found matching the supplied filter - If no routes were defined in the config/routes.rb file, will warn the user with the original message
1 parent c02b856 commit ce13f79

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

actionpack/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* More explicit error message when running `rake routes`. `CONTROLLER` argument
2+
can now be supplied in different ways:
3+
`Rails::WelcomeController`, `Rails::Welcome`, `rails/welcome`
4+
5+
Fixes #22918
6+
7+
*Edouard Chin*
8+
19
* Allow `ActionController::Parameters` instances as an argument to URL
210
helper methods. An `ArguemntError` will be raised if the passed parameters
311
are not secure.

actionpack/lib/action_dispatch/routing/inspector.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def format(formatter, filter = nil)
6565
routes = collect_routes(routes_to_display)
6666

6767
if routes.none?
68-
formatter.no_routes
68+
formatter.no_routes(collect_routes(@routes), filter)
6969
return formatter.result
7070
end
7171

@@ -84,7 +84,8 @@ def format(formatter, filter = nil)
8484

8585
def filter_routes(filter)
8686
if filter
87-
@routes.select { |route| route.defaults[:controller] == filter }
87+
filter_name = filter.underscore.sub(/_controller$/, '')
88+
@routes.select { |route| route.defaults[:controller] == filter_name }
8889
else
8990
@routes
9091
end
@@ -136,17 +137,27 @@ def header(routes)
136137
@buffer << draw_header(routes)
137138
end
138139

139-
def no_routes
140-
@buffer << <<-MESSAGE.strip_heredoc
140+
def no_routes(routes, filter)
141+
@buffer <<
142+
if routes.none?
143+
<<-MESSAGE.strip_heredoc
141144
You don't have any routes defined!
142145
143146
Please add some routes in config/routes.rb.
144-
145-
For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html.
146147
MESSAGE
148+
elsif missing_controller?(filter)
149+
"The controller #{filter} does not exist!"
150+
else
151+
"No routes were found for this controller"
152+
end
153+
@buffer << "For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
147154
end
148155

149156
private
157+
def missing_controller?(controller_name)
158+
[ controller_name.camelize, "#{controller_name.camelize}Controller" ].none?(&:safe_constantize)
159+
end
160+
150161
def draw_section(routes)
151162
header_lengths = ['Prefix', 'Verb', 'URI Pattern'].map(&:length)
152163
name_width, verb_width, path_width = widths(routes).zip(header_lengths).map(&:max)
@@ -187,7 +198,7 @@ def section(routes)
187198
def header(routes)
188199
end
189200

190-
def no_routes
201+
def no_routes(*)
191202
@buffer << <<-MESSAGE.strip_heredoc
192203
<p>You don't have any routes defined!</p>
193204
<ul>

actionpack/test/dispatch/routing/inspector_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ def self.call(env)
77
end
88
end
99

10+
class Rails::DummyController
11+
end
12+
1013
module ActionDispatch
1114
module Routing
1215
class RoutesInspectorTest < ActiveSupport::TestCase
@@ -331,6 +334,41 @@ def test_inspect_routes_shows_resources_route_when_assets_disabled
331334
" cart GET /cart(.:format) cart#show"
332335
], output
333336
end
337+
338+
def test_routes_with_undefined_filter
339+
output = draw(:filter => 'Rails::MissingController') do
340+
get 'photos/:id' => 'photos#show', :id => /[A-Z]\d{5}/
341+
end
342+
343+
assert_equal [
344+
"The controller Rails::MissingController does not exist!",
345+
"For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
346+
], output
347+
end
348+
349+
def test_no_routes_matched_filter
350+
output = draw(:filter => 'rails/dummy') do
351+
get 'photos/:id' => 'photos#show', :id => /[A-Z]\d{5}/
352+
end
353+
354+
assert_equal [
355+
"No routes were found for this controller",
356+
"For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
357+
], output
358+
end
359+
360+
def test_no_routes_were_defined
361+
output = draw(:filter => 'Rails::DummyController') { }
362+
363+
assert_equal [
364+
"You don't have any routes defined!",
365+
"",
366+
"Please add some routes in config/routes.rb.",
367+
"",
368+
"For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
369+
], output
370+
end
371+
334372
end
335373
end
336374
end

0 commit comments

Comments
 (0)