โ Ruby on Rails learning pathRuby on Rails ยท Module 8 of 10
APIs and JSON
Rails can serve JSON APIs for mobile and React apps.
๐ Key ideas
- rails new api --api builds a lean API-only app.
- render json: @product returns JSON.
- Jbuilder or serializers shape the JSON output.
- Use HTTP status codes: 200, 201 created, 404, 422 unprocessable.
- Token auth (e.g. JWT or API keys) replaces cookies for mobile clients.
๐ก Real-life example: Like a canteen that also delivers: the same kitchen serves plates in the hall and packed boxes to other buildings.
--apiCreates an API-only Rails app
render json:Return JSON from an action
422Status for validation errors
JbuilderTemplates that build JSON