Section 3. Refactorings

3.1 respond_to 103.2 CRUD 163.3 Resource 27

3.1 respond_to

When you have two actions that differ primarily in the content type of the returned document, a respond_to refactoring is necessary.

image

3.1.1 Motive

Methods that differ in the content type of the returned document represent unnecessary duplication in your code. You can extract the common preparatory statements into their own method, but your views and API tools still reference a method that you do not need. Using respond_to you can consolidate these two methods into one, consolidating logic and reducing the number of public actions on your controllers.

3.1.2 Steps ...

Get Rails Refactoring to Resources: Using CRUD and REST in Your Rails Application now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.