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

Ruby on rails 4 使用routes.rb命名空间rails的正确方法?,ruby-on-rails-4,routes,Ruby On Rails 4,Routes,我试图为我的应用程序创建一个后端区域,因此我在其中创建了一个名为backend和backend\u controller.rb的文件夹。我需要该文件夹,因为后端区域将有多个文件夹,所以最好将其与其他文件夹分开 我的routes.rb看起来像: namespace :backend do get 'index' end 我的后端\u控制器.rb: class BackendController < ApplicationController def index end en

我试图为我的应用程序创建一个后端区域,因此我在其中创建了一个名为backend和
backend\u controller.rb的文件夹。我需要该文件夹,因为后端区域将有多个文件夹,所以最好将其与其他文件夹分开

我的
routes.rb
看起来像:

namespace :backend do
    get 'index'
end
我的
后端\u控制器.rb

class BackendController < ApplicationController
  def index
  end
end
应用程序/控制器/后端/静态页面\u contter.rb

class BackendController < ApplicationController
end
class Backend::StaticPagesController < BackendController
    def dashboard
    end
end
namespace :backend do
    resource :static_pages, path: '', only: [] do
        root to:'static_pages#dashboard'
end
这很好,但因为我是新手,我必须问一下。这是一个好方法还是一个常规方法?要管理用户可以在后端看到的权限,我使用
backend\u controller.rb
对吗?最后,我必须使用
资源:
而不是
获取“”

那么它就简单了

# routes.rb 
Rails.application.routes.draw do
  namespace :backend, shallow: true do
    resource :backend, path:''
  end
end
然后在你的
应用程序/controllers/backend/backend\u controller.rb中,它看起来是这样的

class Backend::BackendController < ApplicationController
  def index
  end
end
希望这有帮助。

回答您的问题 好的,
namespace:something
scope'something',module'something',as'something'

现在您的声明非常模糊,因为您没有指定控制器。典型的声明如下(假设您有一个控制器
controllers/backend/some\u resources\u controller.rb
,并且希望生成默认路由)

现在你做了什么

namespace :backend
  get 'index'
end
这真的很含糊,我并不奇怪它没有做你想要的。基本上,您只需告诉rails“查看子文件夹‘backend’内部并定义路由‘index’”。面向对象?我们谈论的是哪个文件/控制器

你的
backend\u 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\u controller这样的文件命名名称时,必须将该类重命名为(
/controllers/Backend/Backend\u controller

/controllers/backend\u controller.rb
将不提供任何操作,但将重写ApplicationController以对其进行调优以进行后端访问(但您可能不需要这样做)

路线

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

认证控制器从何而来??问题并没有这样的要求,就像他试图做嵌套控制器,所以我给了他一个例子,实际上我希望我的路线看起来像/后端。我只想访问mysite.com/backend并访问backend的索引页。控制器必须在app>controllers>backend>backend_controller.rbi中,如果我这样做,我会得到错误“uninitialized constant backend::backends controller”,并且路由路径错误,例如:“backend/backends#create”,并且没有路由来索引更新我的答案以进一步反映您的喜好。我在本地环境中重新创建了它,并使用了上面的代码。而不是在routes.rb中获取“index”。定义资源后端或命名空间内的资源后端:backend。。。根据您的要求,如果我只是创建了te路由,但找不到控制器,那么给出错误uninitialize constantmy索引将类似于仪表板或管理员配置文件之类的内容。所以我必须让/controllers/backend_controller.rb路由以覆盖应用程序_控制器,或者在应用程序_控制器上创建操作,对吗?并将所有其他控制器放在后端文件夹中更改classif这样做我有路径/backend/static\u pages/:static\u pages\u id/仪表板(:format),所以我需要传递static\u page\u id,路径mysite.com/backend不会存在对不起,
资源
资源
^^。或者您可以保留此语法并在::collection上添加
。我将进行编辑。为了准确地生成
https://xxx/backend/
并将其重定向到您的控制器操作,您应该使用
根目录:'controller_name#action'
(请参阅我的编辑)
class Backend::BackendController < ApplicationController
/controllers/application_controller.rb
/controllers/backend_controller.rb
/controllers/backend/static_pages_controller.rb
/controllers/backend/***.rb
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
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