Bird
Raised Fist0
Ruby on Railsframework~20 mins

API-only application setup in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rails API Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the default middleware stack in a Rails API-only app?
In a Rails API-only application, which middleware is excluded by default compared to a full Rails app?
ARequest ID middleware is excluded by default.
BStatic file server middleware is excluded by default.
CSession and cookie middleware are excluded by default.
DLogger middleware is excluded by default.
Attempts:
2 left
💡 Hint
Think about what an API-only app does not need compared to a full web app.
📝 Syntax
intermediate
2:00remaining
How to generate a new Rails API-only app?
Which command correctly creates a new Rails API-only application named my_api?
Arails new my_api --only-api
Brails new my_api --api
Crails new my_api --api-only
Drails new my_api --api_mode
Attempts:
2 left
💡 Hint
The flag is short and starts with two dashes.
❓ state_output
advanced
2:00remaining
What is the default response format in Rails API-only controllers?
Given a Rails API-only controller without explicit format settings, what is the default response format when a client requests data?
Ruby on Rails
class BooksController < ApplicationController
  def index
    render json: { books: ['Ruby', 'Rails'] }
  end
end
APlain text
BXML
CHTML
DJSON
Attempts:
2 left
💡 Hint
API-only apps focus on data exchange formats.
🔧 Debug
advanced
2:00remaining
Why does this API-only app raise a Missing CSRF Token error?
Consider this Rails API-only app controller code:
class PostsController < ApplicationController
  protect_from_forgery with: :exception

  def create
    render json: { status: 'ok' }
  end
end
Why does this cause a CSRF token missing error when posting?
Ruby on Rails
class PostsController < ApplicationController
  protect_from_forgery with: :exception

  def create
    render json: { status: 'ok' }
  end
end
AAPI-only apps disable CSRF protection by default; enabling it causes errors without tokens.
BThe controller is missing a before_action to authenticate the user.
CThe render json call is incorrect and causes the error.
DThe routes file is missing the POST route for create.
Attempts:
2 left
💡 Hint
Think about what Rails disables by default in API-only mode.
🧠 Conceptual
expert
2:00remaining
What is the main reason to choose API-only mode in Rails?
Why would a developer choose to create a Rails application with the --api flag instead of a full Rails app?
ATo build a lightweight backend focused on JSON APIs without views or assets.
BTo enable automatic generation of HTML views and layouts.
CTo create a full-stack app with integrated frontend templates.
DTo include all default middleware for session and cookie management.
Attempts:
2 left
💡 Hint
Think about what an API-only app does NOT include.

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

  1. 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.
  2. Step 2: Compare with full Rails app

    Full Rails apps include views and assets, but API-only apps skip these for lightweight backend.
  3. Final Answer:

    To create a lightweight backend that serves JSON responses only -> Option C
  4. 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

  1. Step 1: Recall the Rails new command syntax

    The correct way to create an API-only app is using rails new app_name --api.
  2. Step 2: Match the command with the app name

    Using rails new my_api --api matches the requirement exactly.
  3. Final Answer:

    rails new my_api --api -> Option B
  4. 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

  1. Step 1: Analyze the controller action

    The index action uses render json: to send a JSON response with a message.
  2. Step 2: Understand API-only behavior

    API-only apps respond with JSON, so the output is the JSON string of the hash.
  3. Final Answer:

    {"message":"Hello, API!"} -> Option D
  4. 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

  1. Step 1: Understand the error meaning

    ActionController::UnknownFormat means the controller does not know how to respond to the requested format.
  2. Step 2: Check controller response method

    If the action lacks render json:, Rails can't respond with JSON, causing this error.
  3. Final Answer:

    The controller action does not specify render json: -> Option A
  4. 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
B. Generate views for login and logout pages
C. Use rails new without --api to enable sessions
D. Disable JSON responses to allow HTML forms

Solution

  1. Step 1: Understand API-only app default middleware

    API-only apps exclude middleware for sessions and cookies by default to stay lightweight.
  2. Step 2: Add middleware manually for token auth

    To support token-based authentication using headers, you often need session or cookie middleware added manually.
  3. Final Answer:

    Add middleware for session and cookies support manually -> Option A
  4. Quick Check:

    API-only needs manual middleware for sessions [OK]
Hint: Add session middleware manually for token auth [OK]
Common Mistakes:
  • Expecting views for login in API-only app
  • Recreating full app without --api flag
  • Disabling JSON responses breaks API design
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard