简体   繁体   English

如何要求用户在Rails中更新表单

[英]How to require a user to update a form in Rails

I have a complex page in my Rails app with several nested forms. 我的Rails应用程序中有一个复杂的页面,其中包含多个嵌套表单。 The user can add new records to, or delete records from, the nested forms dynamically. 用户可以动态地向嵌套表单添加新记录或从中删除记录。 (Thanks Railscast #197 ). (感谢Railscast#197 )。

The problem is that the user must submit the form for those changes to "stick". 问题是用户必须将这些更改的表单提交给“ stick”。 And that's not always obvious. 这并不总是很明显。 For example, the user deletes a record from one nested form, and with the magic of jQuery it disappears from the page. 例如,用户从一种嵌套形式中删除一条记录,然后用jQuery的魔力从页面中消失。 But it's not really gone until the user clicks the "Update" button way at the bottom of the page. 但这并没有真正消失,直到用户单击页面底部的“更新”按钮。 If he navigates away from the page, or uses the "Back" button, the changes are lost. 如果他离开页面浏览或使用“返回”按钮,更改将丢失。

My question is, how can I enforce the update: either automatically submitting the form when the user navigates away, or by notifying the user before he does so? 我的问题是,如何执行更新:在用户离开时自动提交表单,还是在用户这样做之前通知用户?

Thanks 谢谢

You can intercept the page before the user leaves this way: 您可以在用户离开之前拦截页面:

window.onbeforeunload = function(){
   alert("no, don't do it!");
   return false
})

If you have the option to save automatically, I'd say use that. 如果您可以选择自动保存,我会说使用它。 It's slick, it's user-friendly and fairly easy to implement. 它很漂亮,易于使用并且易于实施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM