Post Will Not Persist to Database


#1

What’s up ya’ll. I knew if anyone would have an answer or help it would be here. I am using a PHP framework called Laravel as an API and I am having some troubles getting Ember to persist the form post. The form data saves and can be seen in my other route but not in the database, and disappears after refreshing the page. Here is my route - I’ll post my Laravel controller too in case there any Laravel experts here too but I really think the issue is with Ember because I can persist the post when I use the same route using just laravel.

    //ember route
    model() {
        return this.store.createRecord('library');
    },


 actions: {
    saveLibrary() {
        this.get('model').save().then(() => this.transitionToRoute('libraries'));
    },
    willTransition() {
        this.get('model').rollbackAttributes();
    }
}

     //laravel controller
        $statusCode = 200;
        $libraries = $request->all();
        $library = Library::create($libraries);
        $criteria = $library->toArray();
        return Response::json([
            'libraries' => $criteria],
             $statusCode);

#2

Just to add here, after submitting the form Ember doesn’t make it to the transition so the page stays where it is at. I have also tried…

actions: {
    saveLibrary(item) {
        item.save().then( () => this.transitionToRoute('libraries'));
    }
}

I know this is a dumb question but could it be that my model name in Ember is library.js and the pluralized form is librarys, which I need the name libraries to be used for my api?


#3

Do you get any error in your console?

Does the laravel function work? does the request land in you function?


#4

saw your post in the other thread

In your route, why do you call the following?

I’d assume at this point you want to load your libraries into the model of this route? So it would be:

this.get(‘store’).findAll(‘library’);

Is your route named library or libraries? If it’s library the transition wont work because you try to transition to libraries, but I think this would throw an error

Which version of EmberJS/Ember Data are you using?

To the willTransition Event, maybe this might help:

http://emberjs.com/api/classes/Ember.Route.html#event_willTransition


#5

A model is not stored on the route, it’s stored on the corresponding controllers model property (the model property of the route is the method which you override/implement to get the model). So, in your routes action, try

saveLibrary() {
  this.controller.model.save().then(() => {
    this.transitionToRoute('libraries');
  });
}

Ember uses INFLECTOR-JS, so I’m pretty sure they support pluralization of “library”.


#6

@Indr Hey thanks for the input, I made the changes and still not persisting.


#7

@nopyronoparty Yeah sorry about that, I was on mobile and fudged up my reply to another thread. The route is nested in libraries.new (which is supposed to be posting the data), and libraries.index which is where all of the results are showing up and then disappearing.

Thank you for the link btw, I definitely need to brush up on my Ember chops especially transitions. The code doesn’t even make it to the transition, that is what I don’t understand. I am getting no errors so I am really puzzled by all of this.


#8

You need to figure out why you don’t see error messages in the console. Put some console.log statements in your code to trace what’s happening or use your browsers debugger. Another thing is: The routes method to transition is called transitionTo and not transitionToRoute, see http://emberjs.com/api/classes/Ember.Route.html#method_transitionTo. I suspect you’re action never gets called since you don’t get an error logged.


#9

There was a bug in Ember Data 2.8 that swallowed error messages in the console. I’m not sure there has been a release for it yet …


#10

hey guys thanks for the info…I won’t get to work on this until this evening but I will definitely take your suggestions @indr and put some console messages in the code. Also, thank you for clearing up the transitionToRoute(). I can’t thank you enough for your help man.