Ian Jamieson
Ian Jamieson

Reputation: 169

Ember and Sails Create/Save Relationship (BelongsTo/HasMany)

I'm having trouble trying to get ember and sails playing nice together when it comes to relationships with belongsTo/hasMany.

I have a simple form:

<form {{action 'addMessage' on='submit'}}>
<div class="form-group">
    <label for='name'>Title</label>
  {{input value=title class="form-control" required="required"}}
</div>

<div class="form-group">
    <label for='location'>User</label>
    {{input value=user class="form-control" required="required" value=1}}</div>

<p>
    <button class="btn btn-primary btn-block" type="submit">Create Message</button>
</p>

And a controller with the action

actions: {
addMessage: function() {

  var newMessage = this.store.createRecord('message', {
    title: this.get('title'),
    user: this.get('user')
  });
  newMessage.save().then(function() {
  }, function(error) {
    console.warn('Save Failed.', error);
  });
},

I'm just passing a string, and a value which matches a user id. When I look at what's being passed the title is fine, but the user id is null.

I'm using sails ember blueprints so it should work, but think I might be doing something wrong.

I've uploaded the sample code here if someone can take a look https://github.com/jimmyjamieson/ember-sails-example

Upvotes: 1

Views: 137

Answers (2)

Ian Jamieson
Ian Jamieson

Reputation: 169

Ok, fixed. I've added a repo for others to look at. It works with ember and ember-data 2.0 https://github.com/jimmyjamieson/ember-sails-relationships-hasmany-belongsto-example

Upvotes: 1

Andrew
Andrew

Reputation: 1013

On your user input is says value=1 which I think is changing what the controller is writing that property as.

so instead of

{{input value=user class="form-control" required="required" value=1}}

try

{{input value=user class="form-control" required="required"}}

Upvotes: 2

Related Questions