user1287523
user1287523

Reputation: 957

Error saving object in ember.js: 'Object X has no method 'save''

I'm building an application in Rails and Ember.js, and I'm having trouble creating new objects. In my ember controller, when the save action is called, it attempts to call this.get('model').save(). The actual object only requires one parameter, called 'content' to be created. The error I'm getting is Uncaught TypeError: Object xstringx has no method 'save', where xstringx is the actual value I typed into the content textarea box.

this.get('model') is actually returning a string! How can I get it to return my model? Here is some relevant code:

Routes:

App.MessagesNewRoute = Ember.Route.extend({
model: function(){
    this.store.createRecord('message');
},
setupController: function(controller, model){
    controller.set('content', model);
},
renderTemplate: function() {
    this.render('app/templates/messages/new');
}
});

Controller:

App.MessagesNewController = Ember.ObjectController.extend({
actions: {
    save: function(){
    return this.get('model').save().then(function(){
        return _this.transitionToRoute('messages.index');
    });
    }       
}

});

View:

App.MessagesNewView = Ember.View.extend({
templateName: "app/templates/messages/new",
actions: {
    save: function() {
        console.log(arguments);
    }
}
});

New Message Template:

<form role="form">
 <fieldset>
 <div class="form-group">
   <label {{bindAttr for="contentField.field_id"}}>Content</label>
  {{view Ember.TextArea valueBinding='content' class='form-control' name='content' viewName='contentField'}}
</div>
<div class="btn-group">
    <button type="submit" {{action "save"}} class="btn btn-success">Submit</button>
    <button class="btn btn-dancer">Cancel</button>
</div>

Message Model:

App.Message = DS.Model.extend({
content: DS.attr('string'),
user_id: DS.attr('number'),
created_at: DS.attr('string'),
updated_at: DS.attr('string'),
user: DS.belongsTo('user', {async: true}),
priority: DS.attr('number'),
priorityClass: function(user_id){
     //Return the middle size until implemented
    return 'importance-medium';
}.property('priority')
});

Upvotes: 1

Views: 479

Answers (2)

Marcio Junior
Marcio Junior

Reputation: 19128

Not sure if is a typo, but you need to return the data from your model method:

App.MessagesNewRoute = Ember.Route.extend({
    model: function(){
        return this.store.createRecord('message');
    },
    setupController: function(controller, model){
        controller.set('content', model);
    },
    renderTemplate: function() {
        this.render('app/templates/messages/new');
    }
});

And use content.content or model.content like described by Michael

{{view Ember.TextArea valueBinding='model.content' class='form-control' name='content' viewName='contentField'}}

Upvotes: 1

Michael Johnston
Michael Johnston

Reputation: 5320

content is for legacy reasons a reserved word in Ember.

Try binding your textArea to model.content, or changing the attribute name to something else.

{{view Ember.TextArea valueBinding='model.content' class='form-control' name='content' viewName='contentField'}}

Upvotes: 1

Related Questions