There are two ways to implement a controller for your resources. Either derive from
ResourceController or import the
JSONAPI::Resources provides a class,
ResourceController, that can be used as the base class for your controllers.
destroy methods. Just deriving your controller from
ResourceController will give you a fully functional controller.
Of course you are free to extend this as needed and override action handlers or other methods.
A jsonapi-controller generator is avaliable:
JSONAPI::Resources also provides an alternative class to
ResourceControllerMetal. In order to provide a lighter weight controller option this strips the controller down to just the classes needed to work with
Note: This may not provide all of the expected controller capabilities if you are using additional gems such as DoorKeeper.
Additional options can be passed to the serializer using the
serialization_options are passed to the
meta method used to generate resource
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.
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:
The following controllers are used:
You will also need to namespace your routes:
When a namespaced
resource is used, any related
resources must also be in the same namespace.
Error codes are provided for each error object returned, based on the error. These errors are:
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:
By default, all exceptions raised downstream from a resource controller will be caught, logged, and a
By default, when controllers extend functionalities from
ActsAsResourceController#verify_content_type_header method will be triggered before
update_relationship actions. This method is responsible for checking if client’s request corresponds to the correct media type required by JSON API:
In case you need to check the media type for custom actions, just make sure to call the method in your controller’s