I am following this article to learn about writing api using grape gem. But in the response I am not getting the root key. Here is my directory structure,
app
|––controllers
|––api
|––base.rb
|––v1
|––base.rb
|––graduates.rb
In app/controllers/api/v1/graduates.rb:
module API
module V1
class Graduates < Grape::API
include API::V1::Defaults
resource :graduates do
desc "Return all graduates"
get "", root: :graduates do
Graduate.all
end
desc "Return a graduate"
params do
requires :id, type: String, desc: "ID of the
graduate"
end
get ":id", root: "graduate" do
Graduate.where(id: permitted_params[:id]).first!
end
end
end
end end
in app/controllers/api/v1/defaults.rb
module API
module V1
module Defaults
extend ActiveSupport::Concern
included do
prefix "api"
version "v1", using: :path
default_format :json
format :json
formatter :json,
Grape::Formatter::ActiveModelSerializers
helpers do
def permitted_params
@permitted_params ||= declared(params,
include_missing: false)
end
def logger
Rails.logger
end
end
rescue_from ActiveRecord::RecordNotFound do |e|
error_response(message: e.message, status: 404)
end
rescue_from ActiveRecord::RecordInvalid do |e|
error_response(message: e.message, status: 422)
end
end
end
end end
I have used grape-active_model_serializers
gem to serialize like the following,
In app/serializers/graduate_serializer.rb
class GraduateSerializer < ActiveModel::Serializer
attributes :id, :name
end
I got the following output. [{"id":1,"name":"aaaa"},{"id":2,"name":"bbbb"},{"id":3,"name":"cccc"},{"id":4,"name":"dddd"}]
But As I added get "", root: :graduates do
in app/controllers/api/v1/graduates.rb
file, I am expecting the following output,
{"graduates": [{"id":1,"name":"aaaa"},{"id":2,"name":"bbbb"},{"id":3,"name":"cccc"},{"id":4,"name":"dddd"}]}
What I am missing. why graduates
is not added as root. Help me to fix this issue.
Dirty fix: use in app/controllers/api/v1/graduates.rb
get "" do
{ graduates: Graduate.all }
end
Or you can delete gem grape-active_model_serializers
, delete class GraduateSerializer
, delete row formatter :json, Grape::Formatter::ActiveModelSerializers
from defaults.rb
, and add gem grape-entity to Gemfile, install it, add code to app/app/entities/graduate_entity.rb
:
class GraduateEntity < Grape::Entity
root 'graduates', 'graduate'
expose :id
expose :name
end
Change code in app/app/controllers/api/v1/graduates.rb
:
get "" do
present Graduate.all, with: GraduateEntity
end