开发者

rails nested attributes ajax / javascript

I am trying to use the accepts_nested_attributes_for method based on the tutorial by Ryan Ba开发者_StackOverflow中文版tes ( http://railscasts.com/episodes/196-nested-model-form-part-1 ).

For my form, however, I want to have a select list made up of the child elements so that the parent form has a list of children. I then want to dynamically add children and populate them to the select box via ajax. I want those children to be created even if the form submit is aborted.

question - is this possible?

thanks!

EDIT: I have the form populating with the children in the select already. The question is really whether the ajax portion will work. Is it possible to submit just a portion of a form?


Turns out I ended up using the jquery autocomplete combobox (http://jqueryui.com/demos/autocomplete/#combobox) with some modifications. Specifically, the change: function listed looks for invalid selections (which is our case if the user types into the combobox). Here's the default code from the widget:

...
    change: function( event, ui ) {
    if ( !ui.item ) {
        ...
        });
        if ( !valid ) {
            // remove invalid value, as it didn't match anything
            $( this ).val( "" );
            select.val( "" );
            input.data( "autocomplete" ).term = "";
            return false;
        }
    }
}

...

and I changed it to:

    ...
    change: function( event, ui ) {
    if ( !ui.item ) {
        ...
        });
        if ( !valid ) {
            var _new_text = $(this).val()
            var _new_option = '<option value="'+_new_text+'">' + _new_text + '</option>'
            select.prepend(_new_option);//append to combobox
            //now let's select it for submission
            select.children( "option" ).each(function(){
              if( $(this).text().match(_new_text) ){
               this.selected = true;
                return false;
              } 
            })

        }
    }
    }

...

Not sure if there's a better way to do it, but this is where I'm at now! Now working on the filtering of the content in Rails, but that's another story.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜