FredyK
FredyK

Reputation: 289

Ruby Active model serializer with jsonapi , how to characterise links

I am using active model serializer and jsonapi format

I need to get :

{
  "data": {
    "id": "1234",
    "type": "search",
    "relationships": {
      "foo": {
        "data": [
          {
            "id": "12",
            "type": "foo"
          }
        ],
       "links": "/foo/12"
      },        
    }
  },

I have tried several configuration for links but it does not display as above

require 'active_model_serializers'
module test
 class SearchSerializer < ActiveModel::Serializer
   has_one :foo,  data: true, links: {self: true, related: true}
  type 'search'
 end
end

I want to respect the jsonapi format Is anybody with a good example of active model serializer and json_api showing "links" as shwon on above json?

At the moment only the following is displayed

 {"data": {
"id": "1234",
"type": "search",
"relationships": {
  "foo": {
    "data": [
      {
        "id": "12",
        "type": "foo"
      }
    ]
}

},

Note also that I am trying to do that outside the rails framework. Thanks

Upvotes: 0

Views: 2024

Answers (3)

Joshua DeMoss
Joshua DeMoss

Reputation: 131

Going off of FredyK's answer, JSONAPI-SERIALIZER, is a lightweight no-rails serializer for your ruby objects (even though it does have rails integration if desired) which could be a much simpler solution than using Active Record.

Also if you are not using rails, JSONAPI-SERIALIZER pairs really well with the new gem EASY-JSONAPI, which is a middleware, parser, and response validator for JSON:API requests and responses.

Upvotes: 1

Lucas
Lucas

Reputation: 55

Sorry to answer now, but if it is still of anyone interests...

It is quite simple really. First of all it's important to notice that JSON:API specification tell us that the related link should be their URL extension and it's better to show that path through the Search(for that case specific) path, for example: http://localhost:3000/searches/:search_id/foo.

So our SearchSerializer should be something like:

class SearchSerializer < ActiveModel::Serializer
  # The attributes
  attributes :id, :whatever, :something, :another_one

  has_one :foo do
    link(:related) { contact_foo_url(object.id) }
  end
end

Note also that at this point you should include the routes and the controller show method, as similiar to the bellow:

For the routes.rb:

Rails.application.routes.draw do
  resources :searches do
    resource :foo, only: [:show]
    # Also a best practice to dispose a 'relationships' path for this kinda example
    resource :foo, only: [:show], path: 'relationships/foo'
  end
end

And for the FoosController.rb:

class FoosController < ApplicationController
  before_action :set_search

  # GET /searches/1/foo
  def show
    render json: @search.foo
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_search
      @search = Search.find(params[:search_id])
    end
end

Upvotes: 1

FredyK
FredyK

Reputation: 289

After looking what is available in ruby for JSONAPI without rails, I ended using the gem JSONAPI-serializers, It is much easier to set and lighter to load (less dependencies). This fit better with PORO

My serializer becomes

require_relative ./common_serializer module JsonSerializer class SearchSerializer < CommonSerializer attributes :foo, include_links: true def relationship_related_link(attribute_name) nil end end end This gem is much easier to use as the methods which create the json can be changed in the serializer (or in a commonClass)

Upvotes: 0

Related Questions