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) |
ruby
# 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.