1
votes

I am building a rails 5 api with active model serializers to render the JSON objects. I have structured my controllers as follows using namespacing for versions. I'll show one of my resources to display a song.

application_controller.rb (simplified):

class ApplicationController < ActionController::API
  include ActionController::Serialization
end

songs_controller.rb:

class Api::V1::SongsController < ApplicationController
  before_action :set_song, only: [:show, :update, :destroy]
  def show
    authorize @song
    render json: { song: @song }
  end
  private
  def song_params
    params.require(:song).permit(:title, :artist, :band_id)
  end
  def set_song
    @song = Song.find(params[:id])
  end
end

songs_serializer.rb

class SongSerializer < ActiveModel::Serializer
  attributes :id, :title, :band_id
end

The song model is not namespaced into Api::V1. The song model has a few other attributes like artist, created_at, and updated_at that are not included in the serializer so it is my understanding that it will not be included in the JSON sent to the browser app.

My issue is that my application seems to be completely ignoring the song_serializer and is sending JSON that includes all database fields for a song. Any input would be welcomed.

1

1 Answers

0
votes

After banging my head for longer than I care to admit, My issue seemed to be in my songs_controller in the way I was rendering the JSON. Instead of render json: { song: @song } I used render json: @song and it now uses my serializer.