12

How to force rails 4 to reload all route files?
That is without having to restart the application to make the routes from /config/routes/.rb get loaded* I did a split them up in /config/routes/.rb but these /config/routes/ files are not reloaded.

This worked in rails 3 but not 4:

#config.paths['config/routes'].unshift *Dir["config/routes/*.rb"]
Rubytastic
  • 15,001
  • 18
  • 87
  • 175
  • In my Rails 3 project, your code example indeed loads routes from those files, but it does not seem to reload them in development – I need to restart the server to pick up changes. – Henrik N Sep 24 '14 at 14:54

3 Answers3

16

You can use:

Rails.application.reload_routes!

You can read about it here (will have to use find)

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
6
config.paths["config/routes.rb"] << YOUR_ROUTE_FILE
Joe Van Dyk
  • 6,828
  • 8
  • 57
  • 73
  • With a directory of routes (as opposed to a single one per above), `+=` didn't work, but this did: `Dir[Rails.root.join("config/routes/*.rb")].sort.each { |route_file| config.paths["config/routes.rb"] << route_file }` Work in the sense that I no longer get an error message. No idea about whether they reload without restarting the app. Probably not. – Henrik N Feb 26 '15 at 21:51
  • We use `config.paths` in Rails 4 to have multiple route files, everything is reloaded fine in production. – Joe Van Dyk Mar 16 '15 at 15:41
-1

In Rails 3, if you are splitting the routes.rb file into multiple files, we have to add this line to application.rb:

config.paths['config/routes'].concat Dir[Rails.root.join("config/routes/*.rb")]

...and the corresponding routes in config/routes/*.rb files like this:

TestApp::Application.routes.draw do
  namespace :api do
    resources :test
  end
end

In Rails 4, Rails no longer provides a ["config/routes"] key in Rails::Engine.paths. However, in Rails 4, there is no need to add to config.path in application.rb.

Just add the corresponding routes under config/routes/*.rb.

Andrew
  • 227,796
  • 193
  • 515
  • 708
  • 2
    It requires restart of Rails application on every change to /config/routes/*.rb file, changes to /config/routes.rb do work. But my question was aiming towards reloading the /config/routes/*.rb file modifications without a restart. Like you suggest is not true it does not reload by itself! – Rubytastic Oct 18 '13 at 08:12