lundi 26 novembre 2018

Parameters nested in `_jsonapi` when sending from Ember

I have an Ember 3.5 application that is connected to a Rails API app which uses the jsonapi-rails gem.

I'm using the default ember store:

// app/adapters/application.js
import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
  host: 'http://localhost:3000/api/v1'
});

And I have a simple route for a user registration form:

// app/router.js
import EmberRouter from '@ember/routing/router';
import config from './config/environment';

const Router = EmberRouter.extend({
  location: config.locationType,
  rootURL: config.rootURL
});

Router.map(function() {
  this.route('users', function(){
    this.route('user', { path: '/:user_id'});
    this.route('new');
  });
});

export default Router;


But when I attempt to save a record the params are unexpectedly nested in an _jsonapi key on the Rails backend:

app/controllers/api/v1/resource_controller.rb:75:in `permitted_attributes'
app/controllers/api/v1/resource_controller.rb:63:in `create_params'
app/controllers/api/v1/resource_controller.rb:21:in `create'
Started POST "/api/v1/users" for 127.0.0.1 at 2018-11-26 19:20:37 +0100
Processing by API::V1::UsersController#create as JSON
  Parameters: {"_jsonapi"=>{"data"=>{"attributes"=>{"email"=>"test@example.com", "password"=>"[FILTERED]", "password-confirmation"=>"[FILTERED]"}, "type"=>"users"}}}
Completed 400 Bad Request in 2ms (ActiveRecord: 0.0ms)

This is really puzzling since the request payload in Chrome is:

{"data":{"attributes":{"email":"test@example.com","password":"p4ssword","password-confirmation":"p4ssword"},"type":"users"}}




Aucun commentaire:

Enregistrer un commentaire