Controllers
There are two ways to implement a controller for your resources. Either derive from ResourceController
or import the ActsAsResourceController
module.
ResourceController
JSONAPI::Resources
provides a class, ResourceController
, that can be used as the base class for your controllers. ResourceController
supports index
, show
, create
, update
, and destroy
methods. Just deriving your controller from ResourceController
will give you a fully functional controller.
For example:
class PeopleController < JSONAPI::ResourceController |
Of course you are free to extend this as needed and override action handlers or other methods.
A jsonapi-controller generator is avaliable:
rails generate jsonapi:controller contact |
ResourceControllerMetal
JSONAPI::Resources
also provides an alternative class to ResourceController
called ResourceControllerMetal
. In order to provide a lighter weight controller option this strips the controller down to just the classes needed to work with JSONAPI::Resources
.
For example:
class PeopleController < JSONAPI::ResourceControllerMetal |
Note: This may not provide all of the expected controller capabilities if you are using additional gems such as DoorKeeper.
Serialization Options
Additional options can be passed to the serializer using the serialization_options
method.
For example:
class ApplicationController < JSONAPI::ResourceController |
These serialization_options
are passed to the meta
method used to generate resource meta
values.
ActsAsResourceController
JSONAPI::Resources
also provides a module, JSONAPI::ActsAsResourceController
. You can include this module to mix in all the features of ResourceController
into your existing controller class.
For example:
class PostsController < ActionController::Base |
Namespaces
JSONAPI::Resources supports namespacing of controllers and resources. With namespacing, you can version your API.
If you namespace your controller, it will require a namespaced resource.
In the following example, we have a resource
that isn’t namespaced, and one that has now been namespaced. There are slight differences between the two resources, as might be seen in a new version of an API:
class PostResource < JSONAPI::Resource |
The following controllers are used:
class PostsController < JSONAPI::ResourceController |
You will also need to namespace your routes:
Rails.application.routes.draw do |
When a namespaced resource
is used, any related resources
must also be in the same namespace.
Error codes
Error codes are provided for each error object returned, based on the error. These errors are:
module JSONAPI |
These codes can be customized in your app by creating an initializer to override any or all of the codes.
In addition, textual error codes can be returned by setting the configuration option use_text_errors = true
. For example:
JSONAPI.configure do |config| |
Handling Exceptions
By default, all exceptions raised downstream from a resource controller will be caught, logged, and a 500 Internal Server Error
will be rendered. Exceptions can be whitelisted in the config to pass through the handler and be caught manually, or you can pass a callback from a resource controller to insert logic into the rescue block without interrupting the control flow. This can be particularly useful for additional logging or monitoring without the added work of rendering responses.
Pass a block, refer to controller class methods, or both. Note that methods must be defined as class methods on a controller and accept one parameter, which is passed the exception object that was rescued.
class ApplicationController < JSONAPI::ResourceController |
Action Callbacks
verify_content_type_header
By default, when controllers extend functionalities from jsonapi-resources
, the ActsAsResourceController#verify_content_type_header
method will be triggered before create
, update
, create_relationship
and update_relationship
actions. This method is responsible for checking if client’s request corresponds to the correct media type required by JSON API: application/vnd.api+json
.
In case you need to check the media type for custom actions, just make sure to call the method in your controller’s before_action
:
class UsersController < JSONAPI::ResourceController |