You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 7.1.2模块化API路由映射失败:Users::AuthController未初始化

Rails 7.1.2 API-only应用模块化路由映射错误排查

我是Rails新手,正在搭建API-only应用,想要实现如下模块化目录结构:

├── app
│   ├── channels
│   │   └── application_cable
│   │       ├── channel.rb
│   │       └── connection.rb
│   ├── controllers
│   │   ├── application_controller.rb
│   │   └── concerns
│   ├── jobs
│   │   └── application_job.rb
│   ├── mailers
│   │   └── application_mailer.rb
│   ├── models
│   │   ├── application_record.rb
│   │   └── concerns
│   ├── users
│   │   ├── controllers
│   │   │   └── auth_controller.rb
│   │   ├── models
│   │   │   └── user_model.rb
│   │   ├── routes
│   │   │   └── user_routes.rb
│   │   ├── schema
│   │   │   └── request
│   │   │       └── auth_request.rb
│   │   └── usecase
│   │       └── auth_usecase.rb

但遇到路由映射问题,auth_controller.rb无法匹配自定义路由,报错信息如下:

{
    "status": 404,
    "error": "Not Found",
    "exception": "#<ActionController::RoutingError: uninitialized constant Users::AuthController>",
    "traces": {
        "Application Trace": [],
        "Framework Trace": [],
        "Full Trace": []
    }
}

相关代码

auth_controller.rb

module Users
  class AuthController < ActionController::API
    def login
      body = JSON.parse(request.body.read)

      login_request = LoginRequest.new(body[:email],body[:password])
      if login_request.valid?

      else
        render json: { error: 'email or password can\'t be empty' }, status: :bad_request
      end

    rescue JSON::ParserError
      render json: { error: 'Invalid JSON format' }, status: :bad_request
    end
  end
end

user_model.rb

module Users
    class UserModel < ApplicationRecord
      self.table_name = "users"

      def create_user(users)
        save(users)
      end

      def find_user_by_id(id)
      end

      def find_user_by_email_password(email, password)
      end
    end
end

user_routes.rb

module Users
  module Routes
    def self.draw
      Rails.application.routes.draw do
        namespace :users do
            post 'auth/login', to: 'auth#login'
            post 'auth/register', to: 'auth#register'
            post 'auth/reset-password', to: 'auth#reset_password'
            post 'auth/send-otp', to: 'auth#send_otp'
        end
      end
    end
  end
end

auth_request.rb

module Users
  module Schema
    module Request
      LoginRequest = Struct.new(:email,:password) do
        def valid?
          valid_email? && password.present?
        end
      end

      RegisterRequest = Struct.new(:first_name,:last_name,:email,:password,:phone) do
        def valid?
          first_name.present? && last_name.present? && valid_email? && password.present? && phone.present?
        end

        def valid_email?
          email =~ /\A[^@\s]+@[^@\s]+\z/
        end
      end
    end
  end
end

auth_usecase.rb

module Users
  module Usecase
    class AuthUsecase
      def initialize(user_model)
        @user_model = user_model
      end

      def login(email, password)
        _ = user_model.find_user_by_email_password(email,password)
      end

      def register(user)
        user = user_model.create_user(user)
      end
    end
  end
end

config/routes.rb

Rails.application.routes.draw do
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # users routing /v1/users/*
  require_relative '../app/users/routes/user_routes'
  Users::Routes.draw

  # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
  # Can be used by load balancers and uptime monitors to verify that the app is live.
  get "up" => "rails/health#show", as: :rails_health_check

  # Defines the root path route ("/")
  # root "posts#index"
end

求助问题

我使用的是Rails 7.1.2版本,请求帮助解决该错误,并询问是否需要在controllers文件夹内添加模块名。

内容的提问来源于stack exchange,提问作者Byzantine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 13:54:55