I am newbie in rails, then i started try api only app. I want to try modular app like this structure:
├── 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
but I have trouble with these structure which my auth_controller.rb
cant mapping to route I create. below list the code:
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 endend
user_model.rb
module Users class UserModel < ApplicationRecord this.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 endend
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 endend
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 endend
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 endend
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
I always get error like this
{"status": 404,"error": "Not Found","exception": "#<ActionController::RoutingError: uninitialized constant Users::AuthController>","traces": {"Application Trace": [],"Framework Trace": [],"Full Trace": [] }}
can you help me who to solve these error ? and should I make module name controllers inside controllers folder ? I am using Rails 7.1.2