【问题标题】:How to create a route for two namespaces如何为两个命名空间创建路由
【发布时间】:2015-12-29 01:53:39
【问题描述】:

我正在尝试将我的 Rails 项目拆分为普通用户的前端和管理员的后端。因此,我创建了一个命名空间“admin”,以便我可以轻松控制 admin。创建 admin 命名空间后,我更改了路由

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

有人知道怎么做吗?

【问题讨论】:

    标签: ruby-on-rails namespaces


    【解决方案1】:

    如果我理解您的问题,您希望将所有与 admin 相关的内容放入 admin 命名空间,但将所有内容(例如,根页面)留在外面。

    但是在您的路由示例中,您将所有内容都放在了 admin 命名空间中,甚至是根页面。

    所以一般来说,你想要这样的东西:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 2011-02-16
      • 1970-01-01
      • 2018-05-05
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多