class UsersController < ApplicationController def index hashmap_or_array = [{ name: "foo", email: "[email protected]" }] respond_to do |format| format.html { render html: "Hello World" } format.json { render json: hashmap_or_array } end end end
In addition you will need the route:
resources :users, only: [:index]
This will respond in two different ways to requests on /users
:
/users
or /users.html
, it will show an html page with the content Hello World
/users.json
, it will display a JSON object containing:[ { "name": "foo", "email": "[email protected]" } ]
You can omit format.html { render inline: "Hello World" }
if you want to make sure that your route will answer only to JSON requests.