[英]Ruby on Rails - Controller Subdirectory
I am somewhat new to RoR, 我对RoR有点新鲜,
I want to have a structured directory, because project may become big I don't want to have all controllers directly into controllers
directory. 我想要一个结构化的目录,因为项目可能会变大我不希望所有控制器直接进入
controllers
目录。
I would want something as 我想要一些东西
app/
controllers/
application_controller.rb
groupa/
athing_controller.rb
athing2_controller.rb
groupb/
bthing_controller.rb
However when I place in the routes.rb the following: 但是,当我在routes.rb中放置以下内容时:
get 'athing', :to => "groupa/athing#index"
I get the following error on localhost:3000/athing/ : 我在localhost:3000 / athing /上收到以下错误:
superclass mismatch for class AthingController
类AthingController的超类不匹配
Which is like: 这是:
class AthingController < ApplicationController
def index
end
end
Am I missing something? 我错过了什么吗? Can I place subdirectories at all?
我可以放置子目录吗?
Try to use namespace instead: 尝试使用命名空间:
In your routes: 在你的路线:
namespace :groupa do
get 'athing', :to => "athing#index"
end
In your controller: 在你的控制器中:
class Groupa::AthingController < ApplicationController
In browser: 在浏览器中:
localhost:3000/groupa/athing/
In config/routes.rb 在config / routes.rb中
namespace :namespace_name do
resources : resource_name
end
In app/controllers/ 在app / controllers /
create a module name with your namespace_name, in that place your controllers In that controller class name should be like class namespace_name::ExampleController < ApplicationController 使用namespace_name创建一个模块名称,在那个地方你的控制器在那个控制器类名中应该像class namespace_name :: ExampleController <ApplicationController
When you put your controllers (classes) into a subdirectory, Ruby/Rails expects it to subclass from the parent ( module
): 当您将控制器(类)放入子目录时,Ruby / Rails期望它从父(
module
) 子类化 :
#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
I've changed your model / dir names to conform to Ruby snake_case
convention : 我已经更改了你的模型/目录名称以符合Ruby
snake_case
约定 :
- Use
snake_case
for naming directories, eglib/hello_world/hello_world.rb
使用
snake_case
命名目录,例如lib/hello_world/hello_world.rb
- Use
CamelCase
for classes and modules, egclass GroupA
将
CamelCase
用于类和模块,例如class GroupA
Rails routing has the namespace
directive to help: Rails路由有命令
namespace
指令来帮助:
#config/routes.rb
namespace :group_a do
resources :a_thing, only: :index #-> url.com/group_a/a_thing
end
... also the module
directive: ...还有
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
The difference is that namespace
creates a subdir in your routes , module
just sends the path to the subdir-ed controller. 不同之处在于
namespace
在路由中创建了一个子目录, module
只是将路径发送到子目录控制器。
Both of the above require the GroupA::
superclass on your subdirectory controllers. 以上两者都需要子目录控制器上的
GroupA::
superclass。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.