2
votes

I am very new to ember so this may be something that is really simple but all my research has gotten me nowhere. I appreciate the help

I have a very simple app I am trying to practice with. There is a form in my ember front-end where you can create users with a name and a birthdate. On submit I want the data to go to my rails backend and create the user. The data in the name field is posting to the route but the date field is coming back as null. Here is my code

The form

<form {{action "save" on="submit"}}>
<p>
<label>Name:
  {{input value=model.name}}
</label>
</p>
<p>
<label>Date of Birth:
  {{input value=model.dob}}
</label>
</p>
<input type="submit" value="Save"/>
</form>

The Controller

import Ember from 'ember';
export default Ember.Controller.extend({
    actions: {
        save: function() {
            this.get('model').save()
            return false;
        }
    }
});

Parameters from the users#create route in rails

Parameters: {"user"=>{"name"=>"testname", "dob"=>nil}}

edit: Model defenition

import DS from 'ember-data';
export default DS.Model.extend({
    name: DS.attr('string'),
    dob: DS.attr('date')
});

Thank you for any help

2
Could you post your model definition, please.artych
@Artych Added to the postSid W.
Everything works if I don't have data type as date. If I keep it as a string then the rails backend still works as long as a valid date is sent backSid W.

2 Answers

1
votes

I believe Ember couldn't parse your input value (String type) into dob property (Date type).

One of the possible workaround is to extend input helper with settable computed property that parse from String to Date and back. Look here for details: https://stackoverflow.com/a/25871489/4950029.

If you'd like to practice only, just set dob: DS.attr('string') in your model definition and it would work.

0
votes

I had similar issues recently. And it was because of the date serializer of ember-data. Try adding a custom date transform that acts as a pass-through

`import DS from 'ember-data'`

DateTransform = DS.Transform.extend
  deserialize: (serialized) ->
    serialized

  serialize: (deserialized) ->
    deserialized

`export default DateTransform`

If you use ember-cli, which I strongly suggest you do, you can do ember g transform date