1
votes

I have an ember app which has a profiles route to display a title and a description of a particular item from the database. I want to create an edit page for that data, as in CRUD in ember. I implemented an edit route under the profiles resource route.

The index template works fine, it successfully populates the data from the database and the results show in the template. However, as soon as I declare a controller for that route and add an action to that controller the template breaks.

Here is the app.js code for the app.

App.Profile = DS.Model.extend({
title: DS.attr('string'),
description: DS.attr('string')
});

App.Router.map(function(){
this.resource("about", function(){
    this.route("create", {path: '/create'});
});
this.resource('profiles', {path: '/profiles/:profiles_id'}, function(){
    this.route('edit');
});
this.resource('login');
this.resource("logout");
});

App.ProfilesController = Ember.Controller.extend();

App.ProfilesRoute = Ember.Route.extend({
model: function(params) {
    NProgress.start();
    var data = App.Profile.find(params.profiles_id);
    data.then(function(){
      NProgress.done();
    });
    return data;
}
});

The Html Code for profiles:

<script type="text/x-handlebars" data-template-name="profiles">
  <div class="container">
    <div class="row">
      <h1>{{title}}</h1>
  <p>
    {{description}}
  </p>
  <form {{action edit on="submit"}}>
    {{input value=title type="text" placeholder="Title"}}
    {{textarea value=description}}
    {{input class="button" type="submit" value="Edit"}}
  </form>
</div>
  </div>
</script>

At this point everything works fine. but as soon as i add edit action to ProfileController, when i load the page in the browser only the form with no data is shown. what went wrong?

when i add this code to the ProfilesController onyl the form with no data is shown:

App.ProfilesController = Ember.Controller.extend({
    edit: function(){
        var self =this, data = this.getProperties('title', 'description');
        self.set('errorMessage', null);
        self.set('successMsg', null);
        Ember.$.put('profiles', data, function(){
            NProgress.start();
        }).then(function(response){
            NProgress.done();
            if(response.success){

            }else {
                self.set('errorMessage', response.message);
                console.log(response.message);
            }
        });
    }
});

I want to create an action when the form is submitted, the edit action get triggered and do the put on the server, i have php laravel 4 backend at the server to handle the request RESTfully.

I tried to implement the above code under edit template as in profiles/edit template but i couldn't make it work, so i pasted the code in index template of profiles.

Ember displays this log on console

generated -> route:profiles.index Object {fullName: "route:profiles.index"} ember-1.0.0.js:356

Rendering application with default view <(subclass of Ember.View):ember330> Object {fullName: "view:application"} ember-1.0.0.js:356

Rendering profiles with default view Object {fullName: "view:profiles"} ember-1.0.0.js:356

generated -> controller:profiles.index Object {fullName: "controller:profiles.index"} ember-1.0.0.js:356

Could not find "profiles.index" template or view. Nothing will be rendered Object {fullName: "template:profiles.index"} ember-1.0.0.js:356

generated -> route:index Object {fullName: "route:index"} ember-1.0.0.js:356

generated -> route:about.index Object {fullName: "route:about.index"} ember-1.0.0.js:356

1

1 Answers

0
votes

Couple of things:

  1. For putting it inside profiles/edit template, you would need a profiles.hbs template which has {{outlet}} defined. That way edit template will get shown. Also, profiles.hbs should be outside of profiles directory. and profiles directory should contain index and edit templates.

  2. I think it should be an ArrayController instead of just Controller.

I think once you change according to point 1, it should work properly. Give it a try.