Skip to content

JSONAPI :: Create a Singleton Resource Profile

Jay Dorsey edited this page Jan 24, 2019 · 9 revisions

A singleton resource is a api url call that will always lead to only one resource. A real world use case would be "GET /profile" and "PATCH /profile" which (respectively) would get the profile record of the current user and update the record of the current record.

As mentioned in the documentation in the routes section, you need a singular resource route without an id:

config/routes.rb:

jsonapi_resource :profile

You need to record the current logged in user via a context method on one of your controllers or across all controllers using ApplicationController:

def context 
  { user: current_user } 
end

and then you need to override the find_by_key method on the profile resource:

def self.find_by_key(key, options = {})
  context = options[:context]
  model = context[:user]
  fail JSONAPI::Exceptions::RecordNotFound.new(key) if model.nil?
  resource_for_model(model).new(model, context)
end

Note that your controller will also be in the singular profile_controller.rb not `profiles_controller.rb'

Then the singleton calls ( GET /profile, POST /profile, PUT /profile, DELETE /profile ) will work ( although the json api type will still be plural type: 'profiles', )

Note: singletons are not officially supported by the spec, so there may be clients that are not compatible with this approach.

Clone this wiki locally