Junior Track Roadmap

Handling multi-format responses

This course covers how a Rails controller action sends back different responses depending on the format a request resolves to. It starts with respond_to in the app's 404 handler. Then it reads Rails' installed source to show the order used to pick a format: the format parameter, the Accept header and the HTML default. Later lessons cover the no-block shorthand, format.any and its alias format.all, branching on success and failure inside a format block, and a route that forces PNG with defaults.

What you'll learn

  • ✓How respond_to runs only the block matching the resolved format
  • ✓The order Rails follows to choose a request's format
  • ✓When a format call needs a block and when the shorthand is enough
  • ✓How format.any and format.all catch unclaimed formats
  • ✓Branching on success and failure inside a format block
  • ✓Forcing a format at the route with defaults: { format: :png }
Free preview · Lesson 1 2 min read

respond_to do |format|...end, already running in this app's 404 handler

Nested resource routing, the course before this one, traced how a request reaches a controller action. That's half the job. Once an action like Admin::Catalog::Lessons::ImagesController#destroy runs, it has to decide what to send back, and in this app there's often more than one right answer: a…

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...