0
votes

I'm really sorry to ask this when there are a lot of similar posts but none seams to work for me.

/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

config/routes.rb:

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

Error:

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:inblock 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:ininject' 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:inserve' actionpack (5.1.2) lib/action_dispatch/journey/router.rb:46:in block in serve' actionpack (5.1.2) lib/action_dispatch/journey/router.rb:33:ineach' 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:incall' rack (2.0.3) lib/rack/etag.rb:25:in call' rack (2.0.3) lib/rack/conditional_get.rb:25:incall' rack (2.0.3) lib/rack/head.rb:12:in call' rack (2.0.3) lib/rack/session/abstract/id.rb:232:incontext' rack (2.0.3) lib/rack/session/abstract/id.rb:226:in call' actionpack (5.1.2) lib/action_dispatch/middleware/cookies.rb:613:incall' activerecord (5.1.2) lib/active_record/migration.rb:556:in call' actionpack (5.1.2) lib/action_dispatch/middleware/callbacks.rb:26:inblock in call' activesupport (5.1.2) lib/active_support/callbacks.rb:97:in run_callbacks' actionpack (5.1.2) lib/action_dispatch/middleware/callbacks.rb:24:incall' 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:incall' actionpack (5.1.2) lib/action_dispatch/middleware/show_exceptions.rb:31:in call' railties (5.1.2) lib/rails/rack/logger.rb:36:incall_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:inblock 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:intagged' railties (5.1.2) lib/rails/rack/logger.rb:24:in call' actionpack (5.1.2) lib/action_dispatch/middleware/remote_ip.rb:79:incall' actionpack (5.1.2) lib/action_dispatch/middleware/request_id.rb:25:in call' rack (2.0.3) lib/rack/method_override.rb:22:incall' rack (2.0.3) lib/rack/runtime.rb:22:in call' activesupport (5.1.2) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in call' actionpack (5.1.2) lib/action_dispatch/middleware/executor.rb:12:in call' actionpack (5.1.2) lib/action_dispatch/middleware/static.rb:125:incall' rack (2.0.3) lib/rack/sendfile.rb:111:in call' railties (5.1.2) lib/rails/engine.rb:522:incall' puma (3.9.1) lib/puma/configuration.rb:224:in call' puma (3.9.1) lib/puma/server.rb:602:inhandle_request' puma (3.9.1) lib/puma/server.rb:435:in process_client' puma (3.9.1) lib/puma/server.rb:299:inblock in run' puma (3.9.1) lib/puma/thread_pool.rb:120:in `block in spawn_thread'

1
Is it Music or Musics?Sebastian Palma
Is it API or Api?Igor Drozdov
I know its wierd but it is MusicsJavier Ramos
Could you add the rails server error you're getting?Sebastian Palma
resources :music for MusicController; resources :musics for MusicsControllerAndrei

1 Answers

1
votes

You have two options: to change your resources to the plural way :musics (which is recommended) or to change the name of your controller and all the files related (routes, directories, controller class) to the singular way.

In the code you've provided you need to specify your resources route in the plural way, that's why Rails says it doesn't find the API::V1::MusicController, because your controller is called MusicsController, try just adding the missing s to your route:

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