Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rubyonrails-Controller子目录_Ruby On Rails_Ruby_Controller_Routes_Subdirectory - Fatal编程技术网

Ruby on rails rubyonrails-Controller子目录

Ruby on rails rubyonrails-Controller子目录,ruby-on-rails,ruby,controller,routes,subdirectory,Ruby On Rails,Ruby,Controller,Routes,Subdirectory,我对RoR有些陌生 我希望有一个结构化的目录,因为项目可能会变大,我不希望所有的控制器都直接进入controllers目录 我想要一些像这样的东西 app/ controllers/ application_controller.rb groupa/ athing_controller.rb athing2_controller.rb groupb/

我对RoR有些陌生

我希望有一个结构化的目录,因为项目可能会变大,我不希望所有的控制器都直接进入
controllers
目录

我想要一些像这样的东西

app/
    controllers/
          application_controller.rb
          groupa/
                athing_controller.rb
                athing2_controller.rb
          groupb/
                bthing_controller.rb
但是,当我在routes.rb中放置以下内容时:

get 'athing', :to => "groupa/athing#index"
我在本地主机上收到以下错误:3000/athing/

类AthingController的超类不匹配

这就像:

class AthingController < ApplicationController
  def index
  end
end
class AthingController
我错过什么了吗?
是否可以放置子目录?

尝试改用名称空间:

在您的路线中:

namespace :groupa do
  get 'athing', :to => "athing#index"
end
在控制器中:

class Groupa::AthingController < ApplicationController
在config/routes.rb中

namespace :namespace_name do
  resources : resource_name
end
应用程序内/控制器/

在控制器所在的位置,使用名称空间_名称创建模块名称 在该控制器中,类名称应该是 类名称空间\u名称::ExampleController

当您将控制器(类)放入子目录时,Ruby/Rails希望它来自父目录(
module
):

。。。还有
模块
指令:

#config/routes.rb
resources :a_thing, only: :index, module: :group_a #-> url.com/a_thing
scope module: :group_a do
  resources :a_thing, only: :index #->  url.com/a_thing
end
不同之处在于
名称空间
在路由中创建了一个subdir,
模块
只是将路径发送到subdir-ed控制器

以上两种方法都需要子目录控制器上的
GroupA::
超类

#app/controllers/group_a/a_thing_controller.rb
class GroupA::AThingController < ApplicationController
  def index
  end
end

#config/routes.rb
get :a_thing, to: "group_a/a_thing#index" #-> url.com/a_thing
#config/routes.rb
namespace :group_a do
  resources :a_thing, only: :index #-> url.com/group_a/a_thing
end
#config/routes.rb
resources :a_thing, only: :index, module: :group_a #-> url.com/a_thing
scope module: :group_a do
  resources :a_thing, only: :index #->  url.com/a_thing
end