Rails API mode (rails new my-api --api) tạo app gọn hơn — bỏ các middleware và module dành riêng cho browser-based apps.
Khác biệt chính:
| Full Rails | API Mode | |
|---|---|---|
| Base controller | ActionController::Base | ActionController::API |
| Middleware | Sessions, cookies, CSRF, flash | Bỏ sessions/cookies/CSRF |
| View layer | ERB, helpers | Không có |
| Response | HTML + JSON | JSON (mặc định) |
# app/controllers/application_controller.rb (API mode)
class ApplicationController < ActionController::API
endDùng API mode khi xây dựng backend cho SPA (React/Vue) hoặc mobile app. Authentication thường dùng JWT qua Authorization: Bearer ... header thay vì cookie session.
Nếu sau cần thêm views (e.g. admin panel), có thể include lại specific modules của ActionController::Base.
Rails API mode (rails new my-api --api) creates a leaner app — it strips middleware and modules only needed for browser-based apps.
Key differences:
| Full Rails | API Mode | |
|---|---|---|
| Base controller | ActionController::Base | ActionController::API |
| Middleware | Sessions, cookies, CSRF, flash | Sessions/cookies/CSRF removed |
| View layer | ERB, helpers | None |
| Response | HTML + JSON | JSON (default) |
# app/controllers/application_controller.rb (API mode)
class ApplicationController < ActionController::API
endUse API mode when building a backend for a SPA (React/Vue) or mobile app. Authentication typically uses JWT via Authorization: Bearer ... header instead of cookie sessions.
If you later need views (e.g. an admin panel), you can re-include specific ActionController::Base modules.