mercredi 26 juillet 2017

Positing JSON to Rails API from Ember JS

I'm trying to post data from my Ember app to my Rails API for a blog application I'm playing with.

I'm having a few issues, which I think each stem from my initial problem.

First of all, when I try to store the data from my inputs in Ember, I can't seem to store them in any variables. I've tried checking to see if I'm trying to access the values the wrong way but the documents I've been reading (Ember docs, and other stack overflow questions) lead me to believe I'm doing this correctly.

//new.js
import Ember from 'ember';
import DS from 'ember-data';

export default Ember.Route.extend({
    actions:{
        createPost(){
            var created_at = new Date();
            var user = this.store.findRecord('user', 1);
            var title = this.get('title');
            var body = this.get('body');
            console.log(title, body);
            // console.log(user);
            var post = this.store.createRecord('post', {
                title: title,
                body: body,
                created_at: created_at,
                user: user
            })

            post.save();
        }
    }
});

and

//new.hbs

<form>
<strong>Title</strong><br/>
<br/>
<strong>Body</strong><br/>
<br/>

<button type="submit" >Submit</button>
</form>

My output of title and body returns undefined.

Another issue I'm having is when I try post this data to my Rails API, I get a 422 error. It says that there is an unpermitted parameter (created_at) included, so I was wondering if there was a way to exclude parameters from my post request?

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire