为什么我的rails无法自动加载app文件夹?

时间:2019-07-11 09:58:08

标签: ruby-on-rails

我刚刚开始使用rails,现在进行一些基本的演练来处理请求。我已经使用rails new (project_name) --api --skip-active-record和以下视频https://www.youtube.com/watch?v=QojnRc7SS9o创建了该项目,因为我还不想使用数据库,所以我添加了--skip-active-record
...经过大量未初始化的错误,我在application.rb

中添加了这一行
config.after_initialize do 
  # it should be defined in 'application_controller.rb'
  puts "def: #{defined? ApplicationController}" # => nil
end

然后我运行rails s,它表示未定义(又名nil)... 当然,我可以手动请求app文件夹,但似乎只是重新发明了轮子,有什么办法可以解决此问题?

我使用从另一篇文章中搜索到的命令运行的其他信息可能会有所帮助:

>> ruby bin/rails runner "ActiveSupport::Dependencies.autoload_paths.each{|line| puts line}"
F:/Programming/RoR/api_test/app/channels
F:/Programming/RoR/api_test/app/controllers
F:/Programming/RoR/api_test/app/controllers/concerns
F:/Programming/RoR/api_test/app/jobs
F:/Programming/RoR/api_test/app/mailers
F:/Programming/RoR/api_test/app/models
F:/Programming/RoR/api_test/app/models/concerns
F:/Programming/RoR/api_test/test/mailers/previews

编辑:未启动错误的完整错误跟踪:

Started GET "/api/v0/handshake" for ::1 at 2019-07-11 17:24:30 +0800

ActionController::RoutingError (uninitialized constant Api::V0::HandshakeController):

activesupport (5.2.3) lib/active_support/inflector/methods.rb:285:in `const_get'
activesupport (5.2.3) lib/active_support/inflector/methods.rb:285:in `block in constantize'
activesupport (5.2.3) lib/active_support/inflector/methods.rb:281:in `each'
activesupport (5.2.3) lib/active_support/inflector/methods.rb:281:in `inject'
activesupport (5.2.3) lib/active_support/inflector/methods.rb:281:in `constantize'
actionpack (5.2.3) lib/action_dispatch/http/request.rb:88:in `controller_class_for'
actionpack (5.2.3) lib/action_dispatch/http/request.rb:81:in `controller_class'
actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:46:in `controller'
actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:52:in `block in serve'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `each'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `serve'
actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:840:in `call'
rack (2.0.7) lib/rack/etag.rb:25:in `call'
rack (2.0.7) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.7) lib/rack/head.rb:12:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (5.2.3) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (5.2.3) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.3) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.3) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.3) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.3) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.3) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.3) lib/rails/rack/logger.rb:26:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.7) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.3) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/static.rb:127:in `call'
rack (2.0.7) lib/rack/sendfile.rb:111:in `call'
railties (5.2.3) lib/rails/engine.rb:524:in `call'
puma (3.12.1) lib/puma/configuration.rb:227:in `call'
puma (3.12.1) lib/puma/server.rb:660:in `handle_request'
puma (3.12.1) lib/puma/server.rb:474:in `process_client'
puma (3.12.1) lib/puma/server.rb:334:in `block in run'
puma (3.12.1) lib/puma/thread_pool.rb:135:in `block in spawn_thread'

我的控制器文件(路径:app/controllers/api/v0/handshake.rb

module Api
  module V0
    class HandshakeController < ApplicationController
      def index
        render json: {status: 'SUCCESS', message: 'Hello World!'}, status: :ok
      end
    end
  end
end

routes.rb

Rails.application.routes.draw do
  namespace 'api' do
    namespace 'v0' do
      resources :handshake
    end
  end
end

1 个答案:

答案 0 :(得分:0)

好的,我明白了。问题是我将handshake.rb重命名为handshake_controller.rb后,文件名与Rails的自动加载文件结构here不匹配