Amber Framework
WebsiteBlogGithubDiscord
  • Introduction
  • Getting Started
  • Guides
    • Overview
    • Installation
    • Create New App
    • Directory Structure
    • Configuration
    • Docker
    • Controllers
      • Sessions
      • Request & Response Objects
      • Halt!
      • Respond With
      • Params
      • Cookies
      • Filters
      • Flash
      • Redirection
      • CSRF
    • Views
      • Basic View Helpers
      • Form Builder
    • Models
      • Granite
        • Granite's README
        • Migrations
        • Validations
        • Callbacks
        • Associations
        • Querying
        • Bulk Insertions
      • Jennifer
        • Jennifer Docs
        • Migrations
        • Models
    • Routing
      • Pipelines
      • Routes
    • Websockets
      • Channels
      • Sockets
      • JavaScript Client
    • Mailers
    • Testing
      • System Tests
  • Deployment
    • Manual Deploy
    • Digital Ocean
    • Heroku
    • Dokku
  • CLI
    • New
    • Recipes
    • Plugins
    • Generate
    • Database
    • Watch
    • Routes
    • Exec
    • Encrypt
  • Examples
    • Amber Auth
    • Crystal Debug
    • Minimal Configuration
  • Cookbook
    • From Scratch
    • Hello World
    • CORS
    • File Download
    • File Upload
    • Cookies
    • Authenticate
    • JSON API
    • JSON Mapping
    • WebSocket Chat
  • Troubleshooting
  • In Production
  • Contributing
  • Code of Conduct
  • HAVE A QUESTION?
    • Join the Discord
    • Follow on Twitter
    • Submit an issue
Powered by GitBook
On this page
  1. Guides
  2. Controllers

Respond With

Respond to multiple request types easily and conveniently

If we need to render a template to html render("template.slang") works nicely. A lot of times we want to respond with json, xml, text or something else. In those cases, we can use respond_with.

Amber will use 2 methods to determine which response type to use:

  1. The accepts header

  2. A url extension

These are the currently supported response types:

html: "text/html"
json: "application/json"
txt:  "text/plain"
text: "text/plain"
xml:  "application/xml"
js:   "application/javascript"

Usage

class PetController < ApplicationController
  def index
    pets = Pet.all
    respond_with do
      html render("index.slang")
      
      # Each response type also accepts a block
      # The JSON response would be triggered from the following
      # GET /pets/index.json
      # GET /pets/index with the `accepts: application/json` header
      json do 
        pets.to_json
      end
      
      # This is another valid block format
      xml { render("index.xml.slang", layout: false) }
      text { "Here are your pets #{pets.try(&.join(", ")}" }
      txt "Here are your pets #{pets.try(&.join(", ")}"
    end
  end
end

If you do not specify a response type with the accepts header or using the dot notation in the url, you will get the first defined response type. In this example you would get the html response.

PreviousHalt!NextParams

Last updated 1 year ago