0

I'm using rails, and backbone with handlebars, and I have a nested form.

I am trying to create a user, and the user can supply a url. So I have a user model and a url model

I thought I could do something like this

<form id="new_user">
     <label name="username" >username</label>
     <input type="text" name="username" />

     <label name="url" >url</label>
     <input type="text" class="nested urls_attributes" name="url" />


</form>

----------------------update -----------------------------------------

as this is something that will be seldomly used, I've updated my backbone view to create the correct json when I submit the form, so now on submit form, I create the first json, then add the nested object.

var form_json = MyApp.Helpers.Serialize_Form($('form#user_form inputs').not('.nested'));
     form_json['urls_attributes']=MyApp.Helpers.Serialze_Form($('form#user_form inputs.nested'));

this creates the json in the format of

{username: "test", urls_attributes: {url:"http://test"}}

So when I pass that to my controller, I expected that the url would be created when the user is created.

Unfortunately, that doesn't seem to be happening, and I have no errors. The user gets created, but not the nested model of urls.

Any idea why this is happening?

-----------how I make the json----------------------

I convert my form into a json object with

    serialize_objects: function(inputs){
        //this takes form inputs and creates a json string of them
    var o = {};

    $.map(inputs, function(t) {

        if (o[t.name] !== undefined) {
            if (!o[t.name].push) {
                o[t.name] = [o[t.name]];
            }
            o[t.name].push(t.value || '');
        } else {
            o[t.name] = t.value || '';
        }
    });

    return o;

    }

I can't use jquery serializeArray because I'm using jqMobi which doesn't implement serializeArray. But that shouldn't matter for this question.

4

0 回答 0