heartcombo / devise

Flexible authentication solution for Rails with Warden.
http://blog.plataformatec.com.br/tag/devise/
MIT License
23.95k stars 5.55k forks source link

Devise ignores active storage draw_routes config for routes generation. #5400

Open phlegx opened 3 years ago

phlegx commented 3 years ago

Environment

Current behavior

  1. Add devise to Gemfile.
  2. Add config.active_storage.draw_routes = false to config/application.rb.
  3. Draw own active storage routes with example naming as: rails_service_blob.
  4. Start server RAILS_ENV=staging rails s. No error on development environment!
  5. 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?

kshnurov commented 2 years ago

Found a workaround, add this to application.rb:

config.before_configuration do |app|
  ActiveStorage.draw_routes = false
end
rgarver commented 1 year ago

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...

marat-y commented 2 months ago

Still an issue Ruby 3.3.3 Rails 7.1.3.4 Devise 4.9.4

nflorentin commented 1 month ago

I would also be interested in knowing the side effects of Devise.reload_routes = false too.