2015-10-01 53 views
0

我想在前端爲普通用戶和後端的管理員分裂我的Rails項目的路線。因此,我創建了一個命名空間「管理員」,這樣我可以很容易地控制admin.After創建管理員命名空間中的我改變了路線從如何創建兩個命名

Rails.application.routes.draw do 
authenticated :user do 
    root to: 'dashboard#index', as: :authenticated_root 
    end 

    unauthenticated do 
    root to: "home#index" 
    end 

    match '(errors)/:status', to: 'errors#show', constraints: { status: /\d{3}/ }, via: :all 

    devise_for :users, skip: [:registrations] 
    as :user do 
    get 'my/profile/edit' => 'devise/registrations#edit', as: 'edit_user_registration' 
    patch 'my/profile' => 'devise/registrations#update', as: 'user_registration' 
    end 

    resources :users 

    resources :events do 
    patch :archive, :unarchive 
    end 
end 

這個

Rails.application.routes.draw do 
    namespace :admin do 
    authenticated :user do 
     root to: 'dashboard#index', as: :authenticated_root 
    end 

    unauthenticated do 
     root to: "home#index" 
    end 

    match '(errors)/:status', to: 'errors#show', constraints: { status: /\d{3}/ }, via: :all 

    devise_for :users, skip: [:registrations] 
    as :user do 
     get 'my/profile/edit' => 'devise/registrations#edit', as: 'edit_user_registration' 
     patch 'my/profile' => 'devise/registrations#update', as: 'user_registration' 
    end 

    resources :users 

    resources :events do 
     patch :archive, :unarchive 
    end 
    end 
end 

這些變化,我得到這個後頁

Rails::WelcomeController#index as HTML 

有誰知道如何做到這一點?

回答

1

如果我明白你的要求,你想把所有與管理有關的東西放到管理員名字空間中,但是把所有東西(例如根頁面)都放在外面。

但在你的路由比如你把一切管理員命名空間中,即使是根頁面。

所以通常,你想要的東西,如:

Rails.application.routes.draw do 
    namespace :admin do 
    # put admin stuff here 
    end 

    # put everything NOT in the admin interface outside your namespace 
    # you want a root route here. That's the page that'll be displayed by default  
    root to :your_root_stuff 

    # and if you have users who aren't admins, devise and authenticated routes too 
    # ... other stuff 
end