madeindjs/api_on_rails

Serialize generator already adds attributes

notapatch opened this issue · 0 comments

In Chapter 6 - section Serialize Products. We use a generator like so:

rails generate serializer Product title price published

This automatically adds the attributes. However, in the text you are encouraged to add the, already created, attributes. As follows:

Suggested changes

$ rails generate serializer Product title price published
      create  app/serializers/product_serializer.rb

Now let’s add attributes to serialize the product:
Now check the generated product serializer:

Listing 75. app/serializers/product_serializer.rb
class ProductSerializer
  include JSONAPI::Serializer
  attributes :title, :price, :published
end

There you go. It’s no more complicated than that. Everything looks right. Let’s change our controller a little bit.