Skip to content
This repository
Browse code

Merge pull request #9316 from steveklabnik/no_routes

Print message on stderr when no routes are defined.
  • Loading branch information...
commit 33310fbf8f387e450e1ccc29ccf95a149c3d3e29 2 parents 8fd17e0 + d3b836a
Xavier Noria fxn authored
6 actionpack/CHANGELOG.md
Source Rendered
... ... @@ -1,5 +1,11 @@
1 1 ## Rails 4.0.0 (unreleased) ##
2 2
  3 +* Add a message when you have no routes defined to both `rake routes` and
  4 + GET "/rails/info/routes" that lets you know you have none defined and links
  5 + to the Rails Guide on the topic.
  6 +
  7 + *Steve Klabnik*
  8 +
3 9 * Change `image_alt` method to replace underscores/hyphens to spaces in filenames.
4 10
5 11 Previously, underscored filenames became `alt="A_long_file_name_with_underscores"`
25 actionpack/lib/action_dispatch/routing/inspector.rb
@@ -91,6 +91,11 @@ def format(formatter, filter = nil)
91 91
92 92 routes = collect_routes(routes_to_display)
93 93
  94 + if routes.none?
  95 + formatter.no_routes
  96 + return formatter.result
  97 + end
  98 +
94 99 formatter.header routes
95 100 formatter.section routes
96 101
@@ -161,6 +166,16 @@ def header(routes)
161 166 @buffer << draw_header(routes)
162 167 end
163 168
  169 + def no_routes
  170 + @buffer << <<-MESSAGE
  171 +You don't have any routes defined!
  172 +
  173 +Please add some routes in config/routes.rb.
  174 +
  175 +For more information about routes, see the Rails Guide: http://guides.rubyonrails.org/routing.html .
  176 +MESSAGE
  177 + end
  178 +
164 179 private
165 180 def draw_section(routes)
166 181 name_width, verb_width, path_width = widths(routes)
@@ -197,6 +212,16 @@ def section(routes)
197 212 @buffer << @view.render(partial: "routes/route", collection: routes)
198 213 end
199 214
  215 + def no_routes
  216 + @buffer << <<-MESSAGE
  217 +<p>You don't have any routes defined!</p>
  218 +<ul>
  219 +<li>Please add some routes in config/routes.rb.</li>
  220 +<li>For more information about routes, please <a href="http://guides.rubyonrails.org/routing.html">see the Rails Guide</a>.</li>
  221 +</ul>
  222 +MESSAGE
  223 + end
  224 +
200 225 def result
201 226 @view.raw @view.render(layout: "routes/table") {
202 227 @view.raw @buffer.join("\n")
15 railties/test/application/rake_test.rb
@@ -143,6 +143,21 @@ def test_rake_routes_calls_the_route_inspector
143 143 assert_equal "cart GET /cart(.:format) cart#show\n", Dir.chdir(app_path){ `rake routes` }
144 144 end
145 145
  146 + def test_rake_routes_displays_message_when_no_routes_are_defined
  147 + app_file "config/routes.rb", <<-RUBY
  148 + AppTemplate::Application.routes.draw do
  149 + end
  150 + RUBY
  151 +
  152 + assert_equal <<-MESSAGE, Dir.chdir(app_path){ `rake routes` }
  153 +You don't have any routes defined!
  154 +
  155 +Please add some routes in config/routes.rb.
  156 +
  157 +For more information about routes, see the Rails Guide: http://guides.rubyonrails.org/routing.html .
  158 +MESSAGE
  159 + end
  160 +
146 161 def test_logger_is_flushed_when_exiting_production_rake_tasks
147 162 add_to_config <<-RUBY
148 163 rake_tasks do

0 comments on commit 33310fb

Please sign in to comment.
Something went wrong with that request. Please try again.