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

时间:2015-08-06 15:56:32

标签: ruby-on-rails-4 routes

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

我的routes.rb看起来像:

namespace :backend do
    get 'index'
end

我的backend_controller.rb

class BackendController < ApplicationController
  def index
  end
end

但是在这种模式下,Rails将在controllers文件夹中搜索我的backend_controller.rb,而不是在controller&gt;后端。我尝试了很多变化,但是我遇到了路由错误。

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

由于

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

的答案

我已在backend_controller.rb文件夹和app/controller子文件夹中创建app/controller/backend,我创建了static_pages_controller.rb,所有文件如下所示:< / p>

应用/控制器/ backend_controller.rb

class BackendController < ApplicationController
end

应用/控制器/后端/ 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

这样可以正常工作,但是因为我必须要问我在轨道上的新手。这是一种好的或传统的方法吗?管理用户可以在后端看到的权限我使用backend_controller.rb权限?最后我必须使用resource:而不是get ''

2 个答案:

答案 0 :(得分:1)

然后就是

# 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

希望这有帮助。

答案 1 :(得分:1)

回答你的问题

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

的简写

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

namespace :backend do
  resources :some_resources
end

现在你做了什么

namespace :backend
  get 'index'
end

真的很暧昧,我并不感到惊讶它没有做你想做的事情。基本上你只是告诉我们去看看子文件夹&#39;后端&#39;并定义路线&#39;索引&#39;&#34;。哦?我们甚至在讨论哪个文件/控制器?

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这样的文件时,必须重命名该类,如(/controllers/backend/backend_controller

class Backend::BackendController < ApplicationController

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

命名空间:后端做   root to:&#39; backend#dashboard&#39;   获取&#39;仪表板&#39;,以及:&#39; backend#dashboard&#39; #singleular资源 端

你可能真正想要做的一个例子......

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

文件

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

班级/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