Ruby on Rails Separate admin folder, controllers and layouts

1.2k views Asked by At

I am new to Ruby on Rails. I want to have following structure for admin section.

  1. app/controller/admin/admin_controller.rb and all other admin section controller under app/controller/admin/ folder

  2. app/views/layout/admin/admin.html.erb to keep separate html layout for admin section

At the same time i want to use Devise Gem for admin and front end user authentication.

I executed rails g devise:views admin, rails generate devise Admin and rails g controller admin/home index command that created views, model and controller for admin user. Now what routes and other setting i need to add so that ruby could understand that if i type http://localhost:3000/admin/ then i should be redirected to http://localhost:3000/admins/sign_in/ page and after entering correct admin credentials i should redirected to index method of controllers/admin/home_controller.rb

is it also possible to keep singular convention of Devise admin views like admin/sign_in instead of admins/sign_in ?

I have searched a lot but could not get relevant help. Please provide steps to achieve above.

Thanks in advance.

This is how route file looks like

Rails.application.routes.draw do
  namespace :admin do
    get 'home/index'
  end

  devise_for :admins

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root to: "home#index"
end

When i type http://localhost:3000/admin/ then i get below error

enter image description here

1

There are 1 answers

8
MrShemek On BEST ANSWER

Your problem is that you do not have root route defined for /admin.

I have the same URL convention routes in one of the apps and routes.rb looks like this:

Rails.application.routes.draw do

  # Admin part
  devise_for :admins, path: '/admin'

  scope module: :admin, path: '/admin', as: 'admin' do
    root to: 'home#index'
  end

  # Redirect app root to client part
  root to: redirect(path: '/panel', status: 301)

  # Client part
  devise_for :clients, path: '/panel'

  scope module: :panel, path: '/panel', as: 'panel' do
    ...
  end
end