Kafka4PresidentNow
Kafka4PresidentNow

Reputation: 396

Responding JSON according to JSON API in controller

I created an rails api to expose my Bottl class. This class has some methods for solving the 99Bottles problem. The song method, for instance, returns the complete lyrics song. My intention is to return a JSON, according to JSON API containing the response of that method.

require_relative '../bottles_logic/bottles'

class SongsController < ApplicationController
  before_action :set_song, only: [:show, :update, :destroy]

  # GET /songs
  def index
    @songs = ::Bottl.new.song

    render json: @songs
  end
end

Using Postman, I get 200 status and the request payload, however, JSON is not shown due to syntax error (unexpected 'S').

My questions are:

Upvotes: 1

Views: 1051

Answers (1)

Alexey Spiridonov
Alexey Spiridonov

Reputation: 93

What data class are you store in @songs? You must pass the hash to render method, for example, render json: { key: value }. If @songs is an object of your class, you must try to use Serializer or @songs.as_json.

Upvotes: 5

Related Questions