lundi 21 novembre 2016

Json Api Serializer Doesn't Work with Rails

I want to use Rails as an API only backend for EmberJS. I have heard json-api serializer has some advantages over active_model_serializer. So, i wanted to give it a try but that doesn't seem to work.

App is all about this;

rails-api new ShopApp 
rails g scaffold Product name price

Added 2 seed data & json-api serializer gem in gemfile

app/db/seeds.rb

Product.create(name:"House",price:"14687640")
Product.create(name:"Car", price:" 12345")

app/Gemfile

gem 'jsonapi-serializers'
gem 'rails-api'

I, manually have added app/serializers directory and in there app/serializers/product_serializer.rb

#app/serializers/product_serializer.rb
require 'jsonapi-serializers'

class ProductSerializer
  include JSONAPI::Serializer
  attribute :name
  attribute :price
end
JSONAPI::Serializer.serialize(products, is_collection: true)

I start rails server, and it gives me json data that includes created_at & updated_at.What might be the problem, i have gone trough documentation on github but everything seems to be alright.Please help and thank you ^.^




Aucun commentaire:

Enregistrer un commentaire