📖 Scenario: You are building a simple backend API for a to-do list app. The API will only send and receive JSON data without any HTML views.
🎯 Goal: Create a new Rails API-only application with a basic setup ready to build JSON endpoints.
📋 What You'll Learn
Create a new Rails application configured as API-only
Set the application name to todo_api
Configure the application to skip views, helpers, and assets
Add a simple controller to test the API setup
💡 Why This Matters
🌍 Real World
API-only Rails apps are used to build backend services that provide data to frontend apps or mobile apps via JSON.
💼 Career
Many companies build APIs with Rails to separate frontend and backend, making this skill valuable for backend and full-stack developer roles.
Progress0 / 4 steps
1
Create a new Rails API-only application
Run the command to create a new Rails application named todo_api with the --api option to make it API-only.
Ruby on Rails
Hint
Use rails new with the --api flag to create an API-only app.
2
Navigate into the application directory
Change directory into the newly created todo_api folder to start working inside the app.
Ruby on Rails
Hint
Use cd todo_api to enter the app folder.
3
Generate a controller for testing the API
Generate a controller named todos with an action index to test the API setup. Use the Rails generator command.
Ruby on Rails
Hint
Use rails generate controller todos index to create the controller and action.
4
Modify the todos controller to render JSON
Edit the TodosController to have the index action render a JSON response with a message { message: 'API is working' }.
Ruby on Rails
Hint
In TodosController, define index to render json: { message: 'API is working' }.
Practice
(1/5)
1. What is the main purpose of creating a Rails application with the --api flag?
easy
A. To generate a full Rails app with HTML views and assets
B. To enable automatic frontend scaffolding
C. To create a lightweight backend that serves JSON responses only
D. To create a desktop application using Rails
Solution
Step 1: Understand the --api flag purpose
The --api flag creates a Rails app optimized for API use, focusing on JSON responses without HTML views.
Step 2: Compare with full Rails app
Full Rails apps include views and assets, but API-only apps skip these for lightweight backend.
Final Answer:
To create a lightweight backend that serves JSON responses only -> Option C
Quick Check:
API-only app = JSON backend [OK]
Hint: API-only means JSON backend, no HTML views [OK]
Common Mistakes:
Thinking API-only apps generate HTML views
Confusing API-only with full Rails app
Assuming API-only creates frontend code
2. Which command correctly creates a new Rails API-only application named my_api?
easy
A. rails new my_api --full
B. rails new my_api --api
C. rails generate api my_api
D. rails create my_api --api-only
Solution
Step 1: Recall the Rails new command syntax
The correct way to create an API-only app is using rails new app_name --api.
Step 2: Match the command with the app name
Using rails new my_api --api matches the requirement exactly.
Final Answer:
rails new my_api --api -> Option B
Quick Check:
rails new + app name + --api = correct command [OK]
Hint: Use 'rails new app_name --api' to create API apps [OK]
Common Mistakes:
Using --full instead of --api
Using 'generate' instead of 'new'
Incorrect flag like --api-only
3. Given this Rails API-only controller code, what will be the JSON response when accessing /api/greetings?
class Api::GreetingsController < ApplicationController
def index
render json: { message: 'Hello, API!' }
end
end
medium
A. Redirect to root path
B.
Hello, API!
C. Error: Missing view template
D. {"message":"Hello, API!"}
Solution
Step 1: Analyze the controller action
The index action uses render json: to send a JSON response with a message.
Step 2: Understand API-only behavior
API-only apps respond with JSON, so the output is the JSON string of the hash.
Final Answer:
{"message":"Hello, API!"} -> Option D
Quick Check:
render json = JSON output [OK]
Hint: render json sends JSON, not HTML [OK]
Common Mistakes:
Expecting HTML output in API-only app
Assuming missing view causes error
Thinking it redirects automatically
4. You created a Rails API-only app but get an error: ActionController::UnknownFormat when requesting JSON. What is the likely cause?
medium
A. The controller action does not specify render json:
B. The app was created without the --api flag
C. The route is missing in routes.rb
D. The database is not migrated
Solution
Step 1: Understand the error meaning
ActionController::UnknownFormat means the controller does not know how to respond to the requested format.
Step 2: Check controller response method
If the action lacks render json:, Rails can't respond with JSON, causing this error.
Final Answer:
The controller action does not specify render json: -> Option A
Quick Check:
UnknownFormat = missing render json [OK]
Hint: Always use render json in API controllers [OK]
Common Mistakes:
Assuming missing --api flag causes this error
Thinking missing route causes UnknownFormat
Blaming database migration for format errors
5. You want to add authentication to your Rails API-only app using token-based headers. Which setup step is essential to support this in an API-only app?
hard
A. Add middleware for session and cookies support manually