I have a subfolder api
in my controllers folder.
So when I call POST "/api/auth"
I would like the program to get there using rails conventions.
I.E. I don't want to write the route for each call, but to use rails "patent" that makes rails go to CRUD actions understanding the PUT, POST, GET by itself.
So in my routes.rb
I have:
namespace :api do
resources :debts, :auth
end
But when I POST (or GET) with localhost:3000/api/auth
I get:
ActionController::RoutingError (uninitialized constant Api::AuthController)
What am I missing?
Please note that I also need to have many controllers inside the subfolder. Is there a short match
for all?
CodePudding user response:
You have to put your controller in a sub folder too en then do e.g.
# /app/controllers/api/debts_controller.rb
module Api
class DebtsController < ApiController
end
end
# /app/controllers/api/auth_controller.rb
module Api
class AuthController < ApiController
end
end
Then in base controller folder:
# /app/controllers/api_controller.rb
class ApiController < ApplicationController
end
CodePudding user response:
You need to namespace the class also in order to get this working.
The following 2 ways can be used:
module Api
class AuthController < ApplicationController
# Your controller code here
end
end
class Api::AuthController < ApplicationController
# Your controller code here
end
If you have some code that needs to be run for every controller inside the Api namespace, you can make an Api base controller, but it's not necessary.