Output JSON instead of HTML

suggest change
class UsersController < ApplicationController
  def index
    hashmap_or_array = [{ name: "foo", email: "foo@example.org" }]

    respond_to do |format|
      format.html { render html: "Hello World".html_safe }
      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:

[
  {
    "name": "foo",
    "email": "foo@example.org"
  }
]

Omit the format.html branch if the action should respond only to JSON. The example above uses render html:, not render inline:.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents