Ruby on Rails - 控制器子目录

hta*_*oya 5 ruby controller routes ruby-on-rails subdirectory

我对RoR有点新鲜,

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

我想要一些东西

app/
    controllers/
          application_controller.rb
          groupa/
                athing_controller.rb
                athing2_controller.rb
          groupb/
                bthing_controller.rb
Run Code Online (Sandbox Code Playgroud)

但是,当我在routes.rb中放置以下内容时:

get 'athing', :to => "groupa/athing#index"
Run Code Online (Sandbox Code Playgroud)

我在localhost:3000/athing /上收到以下错误:

类AthingController的超类不匹配

这是:

class AthingController < ApplicationController
  def index
  end
end
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?我可以放置子目录吗?

小智 12

尝试使用命名空间:

在你的路线:

namespace :groupa do
  get 'athing', :to => "athing#index"
end
Run Code Online (Sandbox Code Playgroud)

在你的控制器中:

class Groupa::AthingController < ApplicationController
Run Code Online (Sandbox Code Playgroud)

在浏览器中:

localhost:3000/groupa/athing/
Run Code Online (Sandbox Code Playgroud)