Rails 嵌套属性 ajax / javascript

发布于 2024-10-28 17:11:02 字数 406 浏览 2 评论 0原文

我正在尝试使用基于 Ryan Bates 教程的 Accepts_nested_attributes_for 方法( http ://railscasts.com/episodes/196-nested-model-form-part-1)。

然而,对于我的表单,我想要一个由子元素组成的选择列表,以便父表单具有子元素列表。然后我想动态添加子项并通过 ajax 将它们填充到选择框。即使表单提交被中止,我也希望创建这些子项。

问题-这可能吗?

谢谢!

编辑:我已经在选择中填充了子项的表单。问题实际上是 ajax 部分是否有效。是否可以只提交表格的一部分?

I am trying to use the accepts_nested_attributes_for method based on the tutorial by Ryan Bates ( 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

段念尘 2024-11-04 17:11:02

结果我最终使用了 jquery 自动完成组合框(http://jqueryui.com/demos/autocomplete/#组合框)并进行了一些修改。具体来说,列出的change:函数会查找无效的选择(如果用户在组合框中键入,这就是我们的情况)。这是小部件的默认代码:

...
    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;
        }
    }
}

...

我将其更改为:

    ...
    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;
              } 
            })

        }
    }
    }

...

不确定是否有更好的方法来做到这一点,但这就是我现在所处的位置!现在正在研究 Rails 中的内容过滤,但那是另一个故事了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文