Extend a standard Rails application to API

What is the best way to add some API-ish behaviour to a standard Rails application ? I mean, is it possible to add some new actions to a controller or modify the existing ones so that they return JSON (without specifying JSON extension in the URL (like http://myhost.com/books and not http://myhost.com/books.json and be able to return either HTML or JSON in the same time? Thank you.

I think the simplest solution would be to add the following to a new action:

def index

@books = Book.all

render json: @books

end

When trying it with httpi here the response I get:

➜ rails_app git:(master) ✗ http :3000/books

HTTP/1.1 200 OK

Cache-Control: max-age=0, private, must-revalidate

Content-Type: application/json; charset=utf-8

ETag: W/“966ef6fc32020fa85b7250fa20f25208”

Transfer-Encoding: chunked

X-Content-Type-Options: nosniff

X-Frame-Options: SAMEORIGIN

X-Request-Id: 893c63ac-b9e3-40ee-b8f5-7b9938916ca5

X-Runtime: 0.359281

X-XSS-Protection: 1; mode=block

[

{

    "created_at": "2017-12-10T16:06:29.896Z",

    "id": 1,

    "isbn": "12345",

    "title": "Java",

    "updated_at": "2017-12-10T16:06:29.896Z"

}

]