Question

I followed some tutorials to create an API for my app. I put the custom controller which inherits from Devise registration and session controllers. I've looked through tons of questions/answers here and cannot resolve my problem.

My solution works on my Ubuntu VM but not on an ec2 instance. Here's the first line of the new class:

class Api::V1::SessionsController < Devise::SessionsController

and some code from routes.rb:

devise_for :users

namespace :api do
namespace :v1 do
 devise_scope :user do
   post 'registrations' => 'registrations#create', :as => 'register'
   post 'sessions' => 'sessions#create', :as => 'login'
   delete 'sessions' => 'sessions#destroy', :as => 'logout'
 end
 get 'tournaments' => 'tournaments#index', :as => 'tournaments'
end
end

My custom controller is in the Api/V1 directory and appears routed correctly. The error is (with a portion of the trace):

        
ActionController::RoutingError (uninitialized constant Api):
  activesupport (3.2.13) lib/active_support/inflector/methods.rb:230:in `block in constantize'
  activesupport (3.2.13) lib/active_support/inflector/methods.rb:229:in `each'
  activesupport (3.2.13) lib/active_support/inflector/methods.rb:229:in `constantize'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:69:in `controller_reference'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:54:in `controller'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:32:in `call'
  actionpack (3.2.13) lib/action_dispatch/routing/mapper.rb:42:in `call'
  journey (1.0.4) lib/journey/router.rb:68:in `block in call'
  journey (1.0.4) lib/journey/router.rb:56:in `each'
  journey (1.0.4) lib/journey/router.rb:56:in `call'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
  rack-pjax (0.7.0) lib/rack/pjax.rb:12:in `call'
  mongoid (3.1.4) lib/rack/mongoid/middleware/identity_map.rb:34:in `block in call'
  mongoid (3.1.4) lib/mongoid/unit_of_work.rb:39:in `unit_of_work'
  mongoid (3.1.4) lib/rack/mongoid/middleware/identity_map.rb:34:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `catch'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  rack (1.4.5) lib/rack/etag.rb:23:in `call'
  rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
  remotipart (1.2.1) lib/remotipart/middleware.rb:27:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'

And to get the server running, I had to set these flags in the applications.rb:

# Code is not reloaded between requests
config.cache_classes = false

# Full error reports are disabled and caching is turned on
config.consider_all_requests_local       = true
config.action_controller.perform_caching = false
Was it helpful?

Solution

Try this:

devise_scope :user do
  post 'registrations' => 'api/v1/registrations#create', :as => 'register'
  post 'sessions' => 'api/v1/sessions#create', :as => 'login'
  delete 'sessions' => 'api/v1/sessions#destroy', :as => 'logout'
end

namespace :api do
  namespace :v1 do
    get 'tournaments' => 'tournaments#index', :as => 'tournaments'
  end
end

Be sure to restart the server after modifying the routes.rb

Ensure your controllers are in the directory api/v1 (use only small letters)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top