Skip to content

Latest commit

 

History

History
66 lines (52 loc) · 1.33 KB

respond-with.md

File metadata and controls

66 lines (52 loc) · 1.33 KB

Respond With

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 use respond_with.

respond_with Demo

See video on Youtube

Examples Usages

Responding with JSON

class PetController < ApplicationController
  def index
    pets = Pet.all
    respond_with { json pets.to_json }
  end
end

Responding with XML

class PetController < ApplicationController
  def index
    pets = Pet.all
    respond_with do
      xml render("index.xml.slang", layout: false)
    end
  end
end

Responding with Text

class PetController < ApplicationController
  def index
    pets = Pet.all
    respond_with{ text "hello world" }
    end
  end
end

Responding to All

{% hint style="info" %} Put it all together and you can respond to whatever is asked for. {% endhint %}

class PetController < ApplicationController
  def index
    pets = Pet.all
    respond_with do
      html render("index.slang")
      json pets.to_json
      xml render("index.xml.slang", layout: false)
      text "hello world"
    end
  end
end