0

I've extended my Devise controller (see this article Extending Devise Registration Controller) and now I want to edit my routes.

The route works fine as it is but it looks bulky. Is there a way to refactor this code to make it take less lines?

Rails.application.routes.draw do
  devise_for :users, controllers: {
      sessions: "sessions/sessions",
      registrations: "sessions/registrations",
      password: "sessions/passwords",
      confirmations: "sessions/confirmations",
      omniauth: "sessions/omniauth",
      unlocks: "sessions/unlocks",
   }
end
Richard Jarram
  • 899
  • 11
  • 22
  • What is your reasoning for overriding all of those Devise controllers? Is it because you need to add extra attributes (as the link suggests?). – Mark Merritt Mar 31 '19 at 21:48

1 Answers1

1

I'll add the caveat that in my personal opinion, dynamically generated routes can be more trouble than they're worth sometimes.

I believe the following will work though. You can construct a hash out of an array of path names, and then use that to assign your controllers.

  paths = ["sessions", "registrations", etc..]
  routes = paths.each_with_object({}) { |path, h| h[path] = "sessions/#{path}" }
  devise_for :users, controllers: routes
Jonathan Thom
  • 56
  • 1
  • 4