Devise ignores active storage draw_routes config for routes generation.
Environment
- Ruby 2.7.2
- Rails 6.1.4
- Devise 4.8.0
Current behavior
- Add
deviseto Gemfile. - Add
config.active_storage.draw_routes = falsetoconfig/application.rb. - Draw own active storage routes with example naming
as: rails_service_blob. - Start server
RAILS_ENV=staging rails s. No error on development environment! - Crash and error...
/home/user/.rvm/gems/ruby-2.7.2@app/gems/actionpack-6.1.4/lib/action_dispatch/routing/route_set.rb:587:in `add_route': Invalid route name, already in use: 'rails_service_blob'
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here:
https://guides.rubyonrails.org/routing.html#restricting-the-routes-created (ArgumentError)
...
from /home/user/.rvm/gems/ruby-2.7.2@app/gems/railties-6.1.4/lib/rails/application.rb:169:in `reload_routes!'
from /home/user/.rvm/gems/ruby-2.7.2@app/gems/devise-4.8.0/lib/devise/rails.rb:17:in `block in <class:Engine>'
...
Expected behavior
Run Rails server as expected. On development environment all works fine. Without Devise installed, server starts fine on all environments.
Found out that config.eager_load = true in staging and production environment is the difference to development. Setting eager load to false on staging and production starts the server without crash. But it should work also with eager load set to true.
See: https://github.com/heartcombo/devise/blob/5d5636f03ac19e8188d99c044d4b5e90124313af/lib/devise/rails.rb#L17
Setting Devise with config.reload_routes = false prevents server crash. But what are the side effects setting this config to false?
Found a workaround, add this to application.rb:
config.before_configuration do |app|
ActiveStorage.draw_routes = false
end
Confirmed this is still an issue on Rails 7.1.0.alpha. The config.before_configuration didn't work for me, but config.reload_routes = false in the Devise initializer did solve it. I'm not sure about the conditions where having that enabled would be important, but it seems like an odd default. Also weird that on app.reload_routes! that the ActiveStorage setting for draw_routes is ignored. That suggests this might be a Rails bug...