【问题标题】:Uinitialized constant BaseController error when using admin namespace使用管理命名空间时出现未初始化的常量基本控制器错误
【发布时间】:2012-08-08 14:25:27
【问题描述】:

我有一个这样的管理命名空间:

  namespace :admin do
    resources :users
    resources :base
  end

具有以下目录结构:

/app/controllers/
        + admin
            - base_controller.rb
            - users_controller.rb
        - users_controller.rb
        - application_controller.rb

我必须将admin/users_conroller.rb 包裹在module Admin end 中,否则我会收到Uninitialized constant BaseController 错误:

class Admin::BaseController < ApplicationController
end

# Works fine
module Admin
  class UsersController < BaseController
  end
end

# Breaks with error
class Admin::UsersController < BaseController
end

知道为什么会这样吗?使用 Rails 3.2。

【问题讨论】:

    标签: ruby-on-rails ruby routing namespaces


    【解决方案1】:

    命名空间映射到目录,带下划线的文件名是类名的驼峰式。

    class Some::DeeplyNested::BaseActionsController < ApplicationController
    

    需要在app/controllers/some/deeply_nested/base_actions_controller.rb 中才能让rails 找到它。

    在你的代码中,没有app/controllers/base_controller.rb,所以BaseController

    class Admin::UsersController < BaseController
    

    指向 Rails 不知道的任何类。你需要给它管理命名空间(因为你的 BaseController 的类定义也有)

    class Admin::UsersController < Admin::BaseController
    end
    

    以上内容和您问题中的工作代码是一回事

    module Admin
      class UsersController < BaseController
      end
    end
    

    【讨论】:

    • 我遇到了类似的问题,值得注意的是命名空间目录上的大小写应该是小写。
    • @RichardHollis 有没有这种情况是真的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2014-08-25
    • 2018-08-25
    相关资源
    最近更新 更多