Hacktoberfest 2026: die Issues, die Maintainer für den Oktober markiert haben – offen und einsteigerfreundlich. Hacktoberfest-Issues durchsuchen

Serializing an array of resources

Offen
#1,100 6 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
35/100
Issue-Typ
Dokumentation
Klarheit
Muss geklärt werden
Aktivitätsstatus
Veraltet
Tech-Stack
rails, ruby

Rechercherichtung

Beginne mit dem Serializer-Beispiel in der verlinkten Dokumentation und vergleiche es mit der Index-Aktion von ProductsController, die Product.where(visible: true) verwendet. Überprüfe, wie Arrays von ProductResource-Objekten serialisiert werden sollen, und präzisiere anschließend die Dokumentation für den Fall einer gefilterten Collection; fertig ist die Aufgabe, wenn der Array-Workflow eindeutig ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

The documentation shows a clear example on how to do it for one resource:

post = Post.find(1)
JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash(PostResource.new(post, nil))

But how does one serialize an array of resources?

For example, suppose this is my controller:

class ProductsController < ApplicationController
  def index
    @products = Product.where(visible: true)
    render json: JSONAPI::ResourceSerializer.new(ProductResource).serialize_to_hash(ProductResource.new(@products, nil))
  end
  def show
    product = Product.find(params[:id])
    render json: JSONAPI::ResourceSerializer.new(Admin::ProductResource).serialize_to_hash(ProductResource.new(product, nil))
  end
end

GET /products/2
The show method returns 1 resource in JSON API compliant format. Even better, I could remove the entire show method and it will still work because the inherited methods do something similar.

GET /products/
However, the index method returns a 500 error. Removing the index method entirely would partially solve the problem, I get an array back containing ALL products. But what if I need it to return only the active=true products? For that I need an array serializer no?

Suggestions?
Am I missing something?
Is the documentation incomplete?
Have I perhaps not understood how to work with JSON API? I've got the impression the idea is to not add any logic to your controllers at all and let the URLs do all the work?

Vorherrschende Sprache
Ruby
Sterne
2.3k
Forks
547
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Entwicklungsumgebung

  • Kein Dockerfile und keine Docker-Compose-Datei
  • Hat eine Pull-Request-Vorlage
  • Kein Beitragsleitfaden

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus JSONAPI-Resources/jsonapi-resources

Alle Issues in JSONAPI-Resources/jsonapi-resources

Ähnliche Issues

Weitere Issues zu Ruby

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.