ActionController::RoutingError: 未初始化的常量 Api::V1::ApiController [英] ActionController::RoutingError: uninitialized constant Api::V1::ApiController
问题描述
我有用于控制用户任务的 Rails 5 API 项目,但出现以下错误,但并非总是针对相同的控制器和路由.
I have Rails 5 API project for controlling user tasks and I have the following error but not always for the same controller and route.
ActionController::RoutingError: uninitialized constant Api::V1::ApiController
我向您描述了我的项目,以更详细地解释错误.
I describe you a little bit my project to explain in more detail the error.
应用结构
路线
scope module: 'api' do
namespace :v1 do
# => Login routes
scope module: 'login' do
match 'login', to: 'sessions#login', as: 'login', via: :post
end
# => Team routes
scope module: 'team' do
# => no admin routes
resources :tasks, except: [:index] do
collection do
match ':view', to: 'tasks#index', as: 'tasks', via: [:get, :post]
end
end
end
end
end
API 控制器
module Api
class ApiController < ApplicationController
def respond_with_errors(object)
render json: {errors: ErrorSerializer.serialize(object)}, status: :unprocessable_entity
end
end
end
团队负责人
module Api::V1
class Team::TeamController < ApiController
任务控制器
module Api::V1
class Team::TasksController < Team::TeamController
登录控制器
module Api::V1
class Login::LoginController < ApiController
会话控制器
module Api::V1
class Login::SessionsController < Login::LoginController
当我执行登录路由和任务路由之后,我在最后一个路由和团队模块中的所有路由中得到错误.如果我更改项目并保存它(只有一个空格),然后我执行任务路由和登录路由后,我在最后一个路由和登录模块中的所有路由中得到错误.
When I execute login route and after tasks route, I get the error in last route and all the routes in team module. If I change the project and save it (only one blank space) and then I execute tasks route and after login route, I get the error in last route and all the routes in login module.
没有任何意义...
Rails 服务器出现此错误
推荐答案
继承时应该使用正确的常量 - ::Api::ApiController
:
You should be using the right constant while inheriting - ::Api::ApiController
:
module Api::V1
class Team::TeamController < ::Api::ApiController
因为否则它正在搜索Api::V1::ApiController
,但应该搜索Api::ApiController
because otherwise it is searching for Api::V1::ApiController
, but should search for Api::ApiController
这篇关于ActionController::RoutingError: 未初始化的常量 Api::V1::ApiController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!