无法使用路由命名空间的首字母缩写词

时间:2018-01-02 21:57:47

标签: ruby-on-rails ruby-on-rails-5

我正在尝试使用缩写(全部大写)的命名空间,但似乎无法弄明白。我总是得到一个"无法自动加载"错误信息。

以下是一个例子:

routes.rb中:

  namespace 'ASAP' do
    resources :foos
  end

尽快/ foos_controller.rb:

class ASAP::FoosController < ApplicationController
  def index
    head :ok
  end
end

配置/是inflections.rb:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.acronym 'ASAP'
end

错误讯息:

Unable to autoload constant Asap::FoosController, expected app/controllers/asap/foos_controller.rb to define it

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

代码中的所有内容都很好,只需将inflections.rb文件放入config/initializers而不是/config路径来加载。

来自Rails:

# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

而不是在这里:

config
└── inflections.rb

下面:

config
└── initializers
    └── inflections.rb
相关问题