I am using ember-cli with a Rails API backend and am having some issues adding a new record.
I've set up the form and route and am able to retrieve the values from the form, but the save() does not pass these values back to the POST request for Rails, resulting in a blank record being created.
I would appreciate any assistance at all.
Adapter application.js:
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
namespace: 'api'
});
Route:
import Route from '@ember/routing/route';
export default Route.extend({
model() {
//return this.store.createRecord('book');
return {};
},
actions: {
saveBook(newBook) {
var curr = this.store.createRecord('book', {
title: newBook.title,
author: newBook.author,
genre: newBook.genre
});
alert("curr.title = " + curr.title);
curr.save();
this.transitionTo('books');
},
}
});
Template:
<h2>Add Book</h2>
<div class="layout-row">
{{!-- {{book-form model=model buttonLabel="Add Book" action='saveBook'}} --}}
<div class="form-horizontal">
<div class="layout-row">
<div class="layout-column flex-200">
{{paper-input label="Title" value=model.title onChange=(action (mut model.title))}}
</div>
</div>
<div class="layout-row">
<div class="layout-column flex-200">
{{paper-input label="Author" value=model.author onChange=(action (mut model.author))}}
</div>
</div>
<div class="layout-row">
<div class="layout-column flex-200">
{{paper-input label="Genre" value=model.genre onChange=(action (mut model.genre))}}
</div>
</div>
<div class="layout-row">
<button type="submit" {{action "saveBook" model}}>Save</button>
</div>
</div>
</div>
Browser result using Ember Inspector:
{"data":{"id":"31","type":"books","attributes":{"title":null,"author":null,"genre":null}}}
newBookpassed tosaveBookaction is. Hard to tell if it's an issue withpaper-inputorbook-formcomponent otherwise. - jelhan31looks like an auto incremented ID applied your server not like a client-side generated ID, which is usually a GUID. - jelhan