Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create API in Rails 4 - Uninitialized constant Api::V1::UsersController

For a personal project I would like create a Restful web-service in Rails 4.

So, I created my first project using rails-api and add the following code:

routes.rb in MyProject > config directory

MyProject::Application.routes.draw do
    namespace :api, defaults: {format: 'json'} do
        namespace :v1 do
            resources :users
        end
    end
end

users_controller.rb in MyProject > app > controllers > api > v1 > users directory

module Api
    module V1
        class UsersController < ApplicationController
            def index
            end

            def create
            end

            def show
            end

            def update
            end

            def delete
            end
        end 
    end
end

When I start the rails server with the command-line rails s and go to this URL: http://localhost:3000/api/v1/users/show I've got this error:

uninitialized constant Api::V1::UsersController

Rails.root: /Users/Jean/Development/MyProject

Application Trace | Framework Trace | Full Trace activesupport (4.0.4) lib/active_support/inflector/methods.rb:228:in const_get' activesupport (4.0.4) lib/active_support/inflector/methods.rb:228:in block in constantize' activesupport (4.0.4) lib/active_support/inflector/methods.rb:224:in each' activesupport (4.0.4) lib/active_support/inflector/methods.rb:224:ininject' activesupport (4.0.4) lib/active_support/inflector/methods.rb:224:in constantize' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:76:incontroller_reference' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:66:in controller' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:44:incall' actionpack (4.0.4) lib/action_dispatch/journey/router.rb:71:in block in call' actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:ineach' actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:in call' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:674:in call' rack (1.5.2) lib/rack/etag.rb:23:in call' rack (1.5.2) lib/rack/conditionalget.rb:25:incall' rack (1.5.2) lib/rack/head.rb:11:in call' actionpack (4.0.4) lib/action_dispatch/middleware/params_parser.rb:27:incall' activerecord (4.0.4) lib/active_record/query_cache.rb:36:in call' activerecord (4.0.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in call' activerecord (4.0.4) lib/active_record/migration.rb:373:in call' actionpack (4.0.4) lib/action_dispatch/middleware/callbacks.rb:29:inblock in call' activesupport (4.0.4) lib/active_support/callbacks.rb:373:in _run__4323212420903942114__call__callbacks' activesupport (4.0.4) lib/active_support/callbacks.rb:80:inrun_callbacks' actionpack (4.0.4) lib/action_dispatch/middleware/callbacks.rb:27:in call' actionpack (4.0.4) lib/action_dispatch/middleware/reloader.rb:64:in call' actionpack (4.0.4) lib/action_dispatch/middleware/remote_ip.rb:76:in call' actionpack (4.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in call' actionpack (4.0.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in call' railties (4.0.4) lib/rails/rack/logger.rb:38:incall_app' railties (4.0.4) lib/rails/rack/logger.rb:20:in block in call' activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:inblock in tagged' activesupport (4.0.4) lib/active_support/tagged_logging.rb:26:in tagged' activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:intagged' railties (4.0.4) lib/rails/rack/logger.rb:20:in call' actionpack (4.0.4) lib/action_dispatch/middleware/request_id.rb:21:incall' rack (1.5.2) lib/rack/runtime.rb:17:in call' activesupport (4.0.4) lib/active_support/cache/strategy/local_cache.rb:83:incall' rack (1.5.2) lib/rack/lock.rb:17:in call' actionpack (4.0.4) lib/action_dispatch/middleware/static.rb:64:incall' railties (4.0.4) lib/rails/engine.rb:511:in call' railties (4.0.4) lib/rails/application.rb:97:incall' rack (1.5.2) lib/rack/lock.rb:17:in call' rack (1.5.2) lib/rack/content_length.rb:14:incall' rack (1.5.2) lib/rack/handler/webrick.rb:60:in service' /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in service' /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in run' /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in block in start_thread'

I found a lot of posts on Stackoverflow with the same error but the answers didn't help me solve my problem.

Thanks !

like image 935
Jean Lebrument Avatar asked Jul 20 '14 14:07

Jean Lebrument


1 Answers

if you have this path:

MyProject > app > controllers > api > v1 > users

controller should be class Api::V1::Users::UsersController if this:

MyProject > app > controllers > api > v1 > users_controller.rb

then class Api::V1::UsersController

For acronym name API::V1::UsersController instead Api::V1::Users::UsersController use inflectors:

in config/initializers/inflections.rb

 ActiveSupport::Inflector.inflections(:en) do |inflect|
   inflect.acronym 'API'
 end
like image 66
Philidor Avatar answered Sep 23 '22 17:09

Philidor