0

我按照一些教程为我的应用程序创建了一个 API。我放置了从设计注册和会话控制器继承的自定义控制器。我在这里查看了大量的问题/答案,但无法解决我的问题。

我的解决方案适用于我的 Ubuntu VM,但不适用于 ec2 实例。这是新类的第一行:

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

还有一些来自 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

我的自定义控制器位于 Api/V1 目录中,并且路由正确。错误是(带有一部分跟踪):

        
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'

为了让服务器运行,我必须在 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
4

1 回答 1

1

尝试这个:

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

修改routes.rb后一定要重启服务器

确保您的控制器在目录中api/v1(仅使用小写字母)

于 2013-07-26T00:15:48.167 回答