I am trying to save a message model in Emberjs. I am using the JSONAPIAdapter and JSONAPISerilzier.
My post to rails hits the right controller and action as a post, but if I look inside with Pry, the data attributes are not there. My payload:
{"data":{"attributes":{"body":"Why","user_id":"17"},"relationships":{"user":{"data":null},"conversation":{"data":null}},"type":"messages"}}
Content-Type:application/vnd.api+json
Rails params in the Rails console:
{"format"=>"json", "controller"=>"api/v1/messages", "action"=>"create"}
JSONAPI::Resources is a library for creating JSON:API backends using the Ruby on Rails application framework. To try it out, let’s create a web service for rating dishes at restaurants. We’ll call it “Opinion Ate”. First, install Ruby on Rails. Create a new Rails app:
The normalized JSON format that Ember Data expects is a JSON:API document with a couple of additional restrictions. First, it is important to make sure that the type name of a record in the normalized JSON object exactly matches the filename of the model defined for this record type.
The JSON:API spec itself is agnostic about inflection rules, however, Ember Data's own JSONAPISerializer assumes types are plural and it will automatically singularize the types.
In Ember Data, serializers format the data sent to and received from the backend store. By default, Ember Data serializes data using the JSON:API format. If your backend uses a different format, Ember Data allows you to customize the serializer or use a different serializer entirely.
So this may help someone else out.
You need to add support for the mime type for JSONAPI.
Add this to config/initializers/mime_types.rb
Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest application/vnd.api+json )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With