Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post to Rails from Emberjs using JSONAPI adapter, Rails not seeing the params

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"}
like image 698
Paul Byrne Avatar asked Aug 14 '15 09:08

Paul Byrne


People also ask

What is @jsonapi resources in rails?

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:

What is normalized JSON in Ember Data?

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.

Does Ember Data support plural or singular inflection rules in JSON?

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.

What is a serializer in Ember Data?

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.


1 Answers

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 )
like image 194
Paul Byrne Avatar answered Oct 22 '22 02:10

Paul Byrne