gpt4 book ai didi

ruby-on-rails - 带有命名空间的自定义设计类的未初始化常量(NameError)

转载 作者:行者123 更新时间:2023-12-04 17:04:24 27 4
gpt4 key购买 nike

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

我的解决方案适用于我的 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

我的自定义 Controller 位于 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

最佳答案

试试这个:

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后一定要重启服务器

确保您的 Controller 在目录 api/v1 中(只使用小写字母)

关于ruby-on-rails - 带有命名空间的自定义设计类的未初始化常量(NameError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17870658/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com