churros
churros

Reputation: 419

How to use the admin/ route properly?

Im trying to make an app that has a public area and an admin dashboard.

For the public area, Im using only a controller named Public, and for it, a lot of views. Each view is an area of my front-end, and the are and under the folder views/public. They are pages like contact, about us, etc.. But at the end, I dont wanna have the public word on my urls. I just want to vist something like: http://localhost:3000/contact and go to the right place. The same goes to the form inside this contact view.

And I have another problem with the admin. First I have created the Admin controller, with its views: index, login and logout. After that, I did an Events controller, with the views: index, new, edit, show, delete, destroy and update.

So, I dont know if Im thinking this right, but after searching about this subject, I did a scope route like this for the events controller:

scope module: 'admin', path: 'admin' do
    resources :events do    
        member do
            get :delete
          end
      end
  end

I had to put the events views under the folder views/admin and then change the controller classes names, puting Admin:: before their names. Also, the controllers Admin and Events are now under the folder controllers/admin.

The Events controller and its views seem working right. Even the CRUD urls. But I dont know how to put the Admin controller routes under the admin scope. My intention is to make an url like this work: http://localhost:3000/admin/ and it should take me to: admin/admin#index

I have tried to put Admin routes under admin scope too, but it didnt work. It was like this:

scope module: 'admin', path: 'admin' do
    get 'admin/index'
    get 'admin/login'
    get 'admin/logout'
    resources :events do    
        member do
            get :delete
          end
      end
  end

I have tried this too:

scope module: 'admin', path: 'admin' do
    get 'index'
    get 'login'
    get 'logout'
    resources :events do    
        member do
            get :delete
          end
      end
  end

Didnt work either. Am I thinking right about this route thing? If not, what am I missing?

Upvotes: 1

Views: 3797

Answers (2)

Reuben Mallaby
Reuben Mallaby

Reputation: 5767

Specify your public routes

get "/contact",       to: "public#contact",       as: :contact

Separate your admin routes:

namespace :admin do 
    get  :login,    to: "admin#login"
    post :logout,   to: "admin#logout"
    root "admin#root"
end

Upvotes: 0

cefigueiredo
cefigueiredo

Reputation: 738

You need to tell which controller should handle index, login and logout...

If you want a namespaced AdminController like Admin::AdminController, you can do that

namespace :admin do
  controller :admin do
    get '/', action: :index
    get :login
    get :logout
  end
  resources :events
end

It will generate these routes

          Prefix Verb   URI Pattern                      Controller#Action
           admin GET    /admin(.:format)                 admin/admin#index
     admin_login GET    /admin/login(.:format)           admin/admin#login
    admin_logout GET    /admin/logout(.:format)          admin/admin#logout
    admin_events GET    /admin/events(.:format)          admin/events#index
                 POST   /admin/events(.:format)          admin/events#create
 new_admin_event GET    /admin/events/new(.:format)      admin/events#new
edit_admin_event GET    /admin/events/:id/edit(.:format) admin/events#edit
     admin_event GET    /admin/events/:id(.:format)      admin/events#show
                 PATCH  /admin/events/:id(.:format)      admin/events#update
                 PUT    /admin/events/:id(.:format)      admin/events#update
                 DELETE /admin/events/:id(.:format)      admin/events#destroy

Upvotes: 2

Related Questions