Complete the code to create a new Rails API-only application.
rails new my_api_app --[1]The --api flag creates a Rails application optimized for APIs by excluding views and assets.
Complete the sentence explaining why Rails API mode excludes some features.
Rails API mode excludes [1] and assets to make the app smaller and faster.
Rails API mode excludes views and assets because APIs usually send data, not HTML pages.
Fix the error in this Rails API controller method to respond with JSON.
def show user = User.find(params[:id]) render [1]: user end
In API mode, responses are usually JSON, so render json: user sends user data as JSON.
Fill both blanks to configure middleware for a Rails API app.
class Application < Rails::Application config.api_only = [1] config.middleware.use [2] end
Setting config.api_only = true enables API mode. Adding ActionDispatch::Cookies middleware allows cookie support if needed.
Fill all three blanks to create a simple JSON response in a Rails API controller.
class GreetingsController < ApplicationController def hello message = { [1]: [2] } render [3]: message end end
The hash key is greeting, the value is the string "Hello, world!", and render json: message sends JSON response.