# Course 37 - Building Web Apps with Ruby On Rails | Episode 15: Multi-format Controllers and Custom JSON Serialization Page: https://stenobird.com/podcast/cybercode-academy-7578615/course-37-building-web-apps-with-ruby-on-rails-episode-15-multi-format-controllers-and-custom-json-serialization Text version: https://stenobird.com/podcast/cybercode-academy-7578615/course-37-building-web-apps-with-ruby-on-rails-episode-15-multi-format-controllers-and-custom-json-serialization.md Podcast: [CyberCode Academy](https://stenobird.com/podcast/cybercode-academy-7578615) Published: 2026-06-28T06:00:07+00:00 Episode link: https://www.spreaker.com/episode/course-37-building-web-apps-with-ruby-on-rails-episode-15-multi-format-controllers-and-custom-json-serialization--72405526 Audio file: https://dts.podtrac.com/redirect.mp3/api.spreaker.com/download/episode/72405526/designing_lean_and_efficient_api_payloads.mp3 Processing state: not_requested JSON: https://stenobird.com/v1/public/podcasts/cybercode-academy-7578615/episodes/course-37-building-web-apps-with-ruby-on-rails-episode-15-multi-format-controllers-and-custom-json-serialization Duration seconds: 1312 ## Resource In this lesson, you’ll learn about: multi-format responses, JSON serialization, and building clean, reusable Rails API controllers1. Multi-Format Controller ResponsesUsing Ruby on Rails:🔹 Problem: Different clients need different formats Browser → HTML Mobile app → JSON External systems → XML 🔹 Solution: Use respond_to def show @user = User.find(params[:id]) respond_to do |format| format.html format.json { render json: @user } format.xml { render xml: @user } end end 👉 Key Insight One controller action can serve multiple clients efficiently2. How Clients Choose the Format🔹 Methods: HTTP Accept header URL extension (.json, .xml) 🔹 Example:GET /users/1.json 👉 Key Insight The client—not the server—decides the response format3. The Serialization Pipeline🔹 Step 1: Data Preparation Convert model → Ruby hash 🔹 Step 2: Data Transformation Convert hash → JSON string 👉 Key Insight Serialization is a two-step process, not a single action4. as_json vs to_json🔹 as_json: Returns a Ruby hash Used for customization 🔹 to_json: Converts to JSON string 🔹 Best practice:render json: @user 👉 Key Insight Let Rails handle conversion to avoid double encoding5. Why Use render Instead of Manual Conversion❌ Bad:render json: @user.to_json ✅ Good:render json: @user 👉 Key Insight Rails automatically calls serialization methods correctly6. Moving Logic from Controllers to Models🔹 Problem: Controllers become cluttered 🔹 Solution: Customize JSON in the model def as_json(options = {}) super(only: [:id, :name]) end 👉 Key Insight Fat models + skinny controllers = clean architecture7. Filtering Data for Efficiency🔹 Options: only → include specific fields except → exclude fields render json: @user, only: [:id, :email] 👉 Key Insight Send only what the client needs → better performance8. Including Association… ## Actions - request_transcript: `POST https://stenobird.com/v1/public/podcasts/cybercode-academy-7578615/episodes/course-37-building-web-apps-with-ruby-on-rails-episode-15-multi-format-controllers-and-custom-json-serialization/transcription-requests` — Idempotently request low-priority transcript generation for this episode. - read_markdown: `GET https://stenobird.com/podcast/cybercode-academy-7578615/course-37-building-web-apps-with-ruby-on-rails-episode-15-multi-format-controllers-and-custom-json-serialization.md` — Read the agent-friendly Markdown representation of this episode resource. A page view does not enqueue transcription. Agents should invoke `request_transcript` explicitly when they need this episode processed. ## Transcript Full transcripts are not published on public pages unless there is a clear rights basis.