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.
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
Put it all together and you can respond to whatever is asked for.
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