lundi 18 avril 2016

Ember CLI 2.5 and Rails 5 is not serializing relationship and throwing 422 unprocessable entity

I am breaking out of my MEAN shell and attempting to learn CoC platforms such as Ember (2.5) and Rails (5 RC3).

I have a simple blog and I have gotten stuck on attempting to save a 'post' that should belong to a user. Ember doesn't seem to want to serialize the 'user' and neglects to pass it to Rails, who throws an error since the relationship is missing.

The guide I used is this one, http://ift.tt/1Mqyztd

it's most likely something dumb (it always is) but I have spent a good 2 hours on SO and the comments section of the article trying to figure this out.

As mentioned in the article, here is the serializer.

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({

  serialize() {

    const result = this._super(...arguments),
      attr = result.data.attributes || {},
      rel = result.data.relationships || {};

    return Object.keys(rel).reduce(function(acc, elem) {
      const data = rel[elem].data;
      if (data) {
        acc[elem + "_id"] = data.id;
      }
      if (data && data.type) {
        acc[elem + "_type"] = data.type[0].toUpperCase() + data.type.slice(1, -1);
      }
      return acc;

    }, attr);
 }
});

And my component code.

import Ember from 'ember';

export default Ember.Route.extend({
  model: function () {
      return {};
  },
  actions: {

  createPost(newPost) {
    let author = this.store.peekRecord('user', 2);
    const slugDate = moment().format('YYYY-MM-DD');
    let slugTitle = _.chain(newPost.title)
                       .escape()
                       .toLower()
                       .kebabCase()
                       .value();
    let slug = slugTitle + "-" + slugDate;

    let post = this.store.createRecord('post', {
      title: newPost.title,
      body: newPost.body,
      user: author,
      slug: slug,
      votes: 0,
      views: 0
    });

post.save().then(function(post) {
  console.log(post);
})
.catch(function(err) {
  console.log(err);
});
}
}

});

My Models:

post model

import DS from 'ember-data';

export default DS.Model.extend({
  title: DS.attr('string'),
  body: DS.attr('string'),
  user: DS.belongsTo('user', { async: true}),
  slug: DS.attr('string'),
  votes: DS.attr('number'),
  views: DS.attr('number'),

});

user model

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr(),
  email: DS.attr(),
  handle: DS.attr(),
  verified: DS.attr(),
  posts: DS.hasMany('posts', { async: true })
});

My migration/schema file.

ActiveRecord::Schema.define(version: 20160419034842) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "posts", force: :cascade do |t|
    t.string   "title"
    t.string   "body"
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string   "views"
    t.string   "votes"
    t.string   "slug"
  end

  add_index "posts", ["user_id"], name: "index_posts_on_user_id", using: :btree

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "handle"
    t.string   "email"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean  "verified"
  end

  add_foreign_key "posts", "users"
end




Aucun commentaire:

Enregistrer un commentaire