Complete the code to create a new Rails API-only application.
rails new my_api_app --[1]Using --api creates a Rails application optimized for API-only use.
Complete the code to generate a controller in an API-only Rails app.
rails generate controller [1] --no-helper --no-assetsControllers in API apps handle JSON requests; helpers and assets are usually not needed.
Fix the error in the config to enable CORS in an API-only Rails app.
Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins '[1]' resource '*', headers: :any, methods: [:get, :post, :options] end end
Using '*' allows all origins to access the API, which is common in development.
Fill both blanks to configure the API-only app to skip session and cookies middleware.
config.api_only = true config.middleware.delete [1] config.middleware.delete [2]
API-only apps usually remove session and cookies middleware to keep the app lightweight.
Fill all three blanks to create a JSON response in a Rails API controller action.
def index @items = Item.all render json: [1], status: [2], [3]: 'items' end
This renders the @items collection as JSON with HTTP status 200 and sets the root key to 'items'.