未初始化的常量API :: V1 :: MusicController

时间:2017-07-23 04:09:16

标签: ruby ruby-on-rails-3 api rubygems

当有很多类似的帖子但没有接缝可供我使用时,我真的很遗憾。

/app/controllers/api/v1/musics_controller.rb:

class Api::V1::MusicsController < ApplicationController::API
  def index
    music = Music.order('created_at DESC');
    render json: {status: 'SUCCESS', message:'Loaded articles', data:music},status: :ok
  end
end

配置/ routes.rb中:

Rails.application.routes.draw do
  namespace 'api' do
    namespace 'v1' do
      resources :music
    end
  end 
end

错误:

  

activesupport(5.1.2)lib / active_support / inflector / methods.rb:271:in   const_get' activesupport (5.1.2) lib/active_support/inflector/methods.rb:271:in阻止constantize'   activesupport(5.1.2)lib / active_support / inflector / methods.rb:267:in   each' activesupport (5.1.2) lib/active_support/inflector/methods.rb:267:in注入'activesupport   (5.1.2)lib / active_support / inflector / methods.rb:267:in constantize' actionpack (5.1.2) lib/action_dispatch/http/request.rb:82:in controller_class'actionpack(5.1.2)   lib / action_dispatch / routing / route_set.rb:43:in controller' actionpack (5.1.2) lib/action_dispatch/routing/route_set.rb:29:in serve'   actionpack(5.1.2)lib / action_dispatch / journey / router.rb:46:在block in serve' actionpack (5.1.2) lib/action_dispatch/journey/router.rb:33:in每个'actionpack(5.1.2)中   lib / action_dispatch / journey / router.rb:33:in serve' actionpack (5.1.2) lib/action_dispatch/routing/route_set.rb:832:in call'rack(2.0.3)   lib / rack / etag.rb:25:in call' rack (2.0.3) lib/rack/conditional_get.rb:25:in call'rack(2.0.3)   lib / rack / head.rb:12:in call' rack (2.0.3) lib/rack/session/abstract/id.rb:232:in context'rack(2.0.3)   lib / rack / session / abstract / id.rb:226:in call' actionpack (5.1.2) lib/action_dispatch/middleware/cookies.rb:613:in call'activerecord   (5.1.2)lib / active_record / migration.rb:556:在call' actionpack (5.1.2) lib/action_dispatch/middleware/callbacks.rb:26:in块中   调用'activesupport(5.1.2)lib / active_support / callbacks.rb:97:in   run_callbacks' actionpack (5.1.2) lib/action_dispatch/middleware/callbacks.rb:24:in调用'actionpack   (5.1.2)lib / action_dispatch / middleware / executor.rb:12:in call' actionpack (5.1.2) lib/action_dispatch/middleware/debug_exceptions.rb:59:in call'   actionpack(5.1.2)   lib / action_dispatch / middleware / show_exceptions.rb:31:in call' railties (5.1.2) lib/rails/rack/logger.rb:36:in call_app'railties   (5.1.2)lib / rails / rack / logger.rb:24:in block in call' activesupport (5.1.2) lib/active_support/tagged_logging.rb:69:in block in tagged'   activesupport(5.1.2)lib / active_support / tagged_logging.rb:26:in   tagged' activesupport (5.1.2) lib/active_support/tagged_logging.rb:69:in标记为'railties(5.1.2)   lib / rails / rack / logger.rb:24:in call' actionpack (5.1.2) lib/action_dispatch/middleware/remote_ip.rb:79:in call'actionpack   (5.1.2)lib / action_dispatch / middleware / request_id.rb:25:in call' rack (2.0.3) lib/rack/method_override.rb:22:in call'rack(2.0.3)   lib / rack / runtime.rb:22:在call' activesupport (5.1.2) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in 中调用'actionpack(5.1.2)   lib / action_dispatch / middleware / executor.rb:12:在call' actionpack (5.1.2) lib/action_dispatch/middleware/static.rb:125:in呼叫'机架中   (2.0.3)lib / rack / sendfile.rb:111:在call' railties (5.1.2) lib/rails/engine.rb:522:in调用'puma(3.9.1)   lib / puma / configuration.rb:224:in call' puma (3.9.1) lib/puma/server.rb:602:in handle_request'puma(3.9.1)   lib / puma / server.rb:435:在process_client' puma (3.9.1) lib/puma/server.rb:299:in块中运行'puma(3.9.1)   lib / puma / thread_pool.rb:120:在`spawn_thread中的块'

1 个答案:

答案 0 :(得分:0)

您有两种选择:将资源更改为复数方式:musics(建议使用)或将控制器名称和所有相关文件(路由,目录,控制器类)更改为单数方式。

在您提供的代码中,您需要以复数方式指定resources路由,这就是Rails说它找不到API::V1::MusicController的原因,因为您的控制器名为MusicsController,请尝试将缺少的s添加到您的路线中:

Rails.application.routes.draw do
  namespace 'api' do
    namespace 'v1' do
      resources :musics
    end
  end 
end