使用 routes.rb 命名空间 rails 的正确方法?

correct way to use routes.rb namespace rails?

我正在尝试为我的应用程序创建一个后端区域,因此我创建了一个名为 backend 的文件夹,并在其中创建了 backend_controller.rb。我需要这个文件夹,因为后端区域会有多个文件夹,所以它和我的其他文件夹分开更好。

我的 routes.rb 看起来像:

namespace :backend do
    get 'index'
end

我的backend_controller.rb:

class BackendController < ApplicationController
  def index
  end
end

但在这种模式下 Rails 将在控制器文件夹中搜索我的 backend_controller.rb,而不是在控制器>后端中。我尝试了很多变体,但我遇到了路由错误。

那么正确的做法是什么?将路径 /backend 设置为索引操作而不是 /backend/index?

谢谢


我做了什么:
基于所有答案,主要是来自 Cyril DD

的答案

我在 app/controller 文件夹和子文件夹 app/controller/backend 中创建了 backend_controller.rb 我创建了 static_pages_controller.rb 并且所有文件如下所示:

app/controllers/backend_controller.rb:

class BackendController < ApplicationController
end

app/controller/backend/static_pages_contter.rb:

class Backend::StaticPagesController < BackendController
    def dashboard
    end
end

routes.rb:

namespace :backend do
    resource :static_pages, path: '', only: [] do
        root to:'static_pages#dashboard'
end

这很好用,但因为我是 rails 的新手,所以我必须问一下。这是一种好的方法还是传统的方法?要管理用户可以在后端看到的权限,我使用 backend_controller.rb 对吗?最后我必须使用 resource: 而不是 get ''

那就简单了

# routes.rb 
Rails.application.routes.draw do
  namespace :backend, shallow: true do
    resource :backend, path:''
  end
end

然后在你的 app/controllers/backend/backend_controller.rb 中,它看起来像这样。

class Backend::BackendController < ApplicationController
  def index
  end
end

当我使用 rake routes 时,它显示

          Prefix Verb   URI Pattern                 Controller#Action
backend_backends GET    /backend(.:format)          backend/backends#index

希望这对您有所帮助。

正在回答您的问题

好的,namespace :somethingscope 'something', module: 'something', as: 'something'

的 shorthand

现在你的声明非常模糊,因为你没有指定控制器。典型的声明看起来像(假设你有一个控制器 controllers/backend/some_resources_controller.rb 并且你想生成默认路由)

namespace :backend do
  resources :some_resources
end

现在你做了什么

namespace :backend
  get 'index'
end

真的很模棱两可,我并不惊讶它没有做你想做的事。基本上你只是告诉 rails 到 "look inside subfolder 'backend' and define the route 'index'"。哦?我们甚至在谈论哪个 file/controller?

你的 backend_controller.rb 应该做什么?它是某种控制面板吗?仪表盘 ?如果是这样,您可能会有很多非 CRUD 操作,但无论如何您应该使用以下语法

namespace :backend
  # Below line of code will auto-generate the `index` for /backend/backend_controller
  resource :backend, only: [:index], path: '' do # we need " path: '' " otherwise we'll have https://xxx/backend/backend/dashboard
    # If you have non-CRUD actions, put them here !
    get 'dashboard' # https://xxx/backend/dashboard
    ...
  end
  # However, this will create URLs like "https://xxx/backend/dashboard", etc.
  # If you want to redirect https://xxx/backend/ to your backend_controller#index, use root
  root to: 'backend#index' # https://xxx/backend/
end

其他人提到的最后一件事,当您在 /backend/ 子文件夹中为 Backend_controller 之类的文件命名空间时,您必须将 class 重命名为 (/controllers/backend/backend_controller)

class Backend::BackendController < ApplicationController

备注:如果您只有一两个控制器操作,而不是使用 resource 方法,您可以声明单一资源

命名空间:后端做 根到:'backend#dashboard' get 'dashboard', to: 'backend#dashboard' # 单一资源 结束


您可能真正想做的事情的示例...

我不确定你是否清楚自己想做什么。例如,这是我的架构

文件

/controllers/application_controller.rb
/controllers/backend_controller.rb
/controllers/backend/static_pages_controller.rb
/controllers/backend/***.rb

class /controllers/backend_controller.rb 不会执行任何操作,但会覆盖 ApplicationController 以针对后端访问对其进行调整(但也许您不需要这样做)

class BackendController < ApplicationController
  # Do you need to change user_access method ? Or any other backend-wide config ?
  # If so put this config here, otherwise leave empty
end

现在,对于 /backend/ 子文件夹中的每个文件,我都继承了 backend_controller

class Backend::StaticPagesController < BackendController
  def index
  end

  # Note : if your index is some kind of dashboard, instead I would declare
  def dashboard
  end
end

class Backend::SomeResourcesController < BackendController
  ...
end

路线

namespace :backend do
  root to 'static_pages#index' # https://xxxx/backend/
  resource :static_pages, only: [:index], path: '' # https://xxxx/backend/index
  resources :some_resources
end

如果您在控制器中选择仪表板解决方案,请改为:

namespace :backend do
  root to: static_pages#dashboard # https://xxxx/backend/
  resource :static_pages, path: '', only: [] do
    get 'dashboard' # https://xxxx/backend/dashboard
  end
  resources :some_resources
end