Jonathan Tuzman
Jonathan Tuzman

Reputation: 13262

Create root and routes for Devise object

I have created an Admin model with the Devise gem. Using the Devise controller generator, I now have a app/controllers/admins folder containing all the stock controllers for me to modify if I choose, such as sessions_controller, passwords_controller, etc.

However, I can't figure out how to just get an Admin controller and simple admin routes like admin_path or new_admin_path.

Here's my rake routes | grep admin

               new_admin_session GET      /admin/sign_in(.:format)                                                                 admins/sessions#new
                   admin_session POST     /admin/sign_in(.:format)                                                                 admins/sessions#create
           destroy_admin_session DELETE   /admin/sign_out(.:format)                                                                admins/sessions#destroy
              new_admin_password GET      /admin/password/new(.:format)                                                            devise/passwords#new
             edit_admin_password GET      /admin/password/edit(.:format)                                                           devise/passwords#edit
                  admin_password PATCH    /admin/password(.:format)                                                                devise/passwords#update
                                 PUT      /admin/password(.:format)                                                                devise/passwords#update
                                 POST     /admin/password(.:format)                                                                devise/passwords#create
                      admin_root GET      /admin(.:format)                                                                         admins/sessions#portal
                  admin_sign_out GET      /admin/sign_out(.:format)                                                                admin/sessions#destroy

And here are the relevant parts of my routes.rb

  devise_for :admins, path: 'admin', controllers: { sessions: 'admins/sessions' }

  devise_scope :admin do
    get "/admin", to: 'admins/sessions#portal', as: 'admin_root'
    get "/admin/sign_out", to: 'admin/sessions#destroy', as: 'admin_sign_out'
  end

You'll see that I've currently got a portal method in my Admin::SessionsController, which is my current workaround. I know the right place for that page is in an AdminsController but I can't figure out how to set that up.

Adding admins: 'admins/admins' to the devise_for :admins, controllers: block doesn't give me any new routes. I tried adding an AdminsController with methods but that doesn't help either, trying to go to /admin/new or /admins/new says no route matches.

Upvotes: 1

Views: 489

Answers (1)

MZaragoza
MZaragoza

Reputation: 10111

This is how I have my device and namespaces set up

# config/routes.rb
Rails.application.routes.draw do

  devise_for :admins, :controllers => { registrations:  'admins/registrations',
                                        sessions:       'admins/sessions',
                                        passwords:      'admins/passwords',
                                        confirmations:  'admins/confirmations'
  }
  authenticate :admin do
    namespace :admins do
      ...
      root 'dashboards#index'
    end
  end
  ...
  root 'pages#index'

Now controllers are also important.

#app/controllers/admin_controller.rb
class AdminController < ApplicationController
  layout 'admins/application'
  before_filter :authenticate_admin!
end

The Devise controllers I have them set like this

#app/controllers/admins/sessions_controller.rb
class Admins::SessionsController < Devise::SessionsController
  layout 'admins/application'
  ...
end

Repeat this process for all your other device controllers

Let me know if this helps you out

Upvotes: 2

Related Questions