当有很多类似的帖子但没有接缝可供我使用时,我真的很遗憾。
/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:ineach' activesupport (5.1.2) lib/active_support/inflector/methods.rb:267:in
注入'activesupport (5.1.2)lib / active_support / inflector / methods.rb:267:inconstantize' 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:incontroller' 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:inserve' actionpack (5.1.2) lib/action_dispatch/routing/route_set.rb:832:in
call'rack(2.0.3) lib / rack / etag.rb:25:incall' rack (2.0.3) lib/rack/conditional_get.rb:25:in
call'rack(2.0.3) lib / rack / head.rb:12:incall' rack (2.0.3) lib/rack/session/abstract/id.rb:232:in
context'rack(2.0.3) lib / rack / session / abstract / id.rb:226:incall' 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:inrun_callbacks' actionpack (5.1.2) lib/action_dispatch/middleware/callbacks.rb:24:in
调用'actionpack (5.1.2)lib / action_dispatch / middleware / executor.rb:12:incall' 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:incall' railties (5.1.2) lib/rails/rack/logger.rb:36:in
call_app'railties (5.1.2)lib / rails / rack / logger.rb:24:inblock 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:intagged' activesupport (5.1.2) lib/active_support/tagged_logging.rb:69:in
标记为'railties(5.1.2) lib / rails / rack / logger.rb:24:incall' 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:incall' 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:incall' 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中的块'
答案 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