如果表单很大,应该多久保存一次数据

时间:2022-08-11 16:59:19

I have a doubt on saving data in database using ajax. (I can do this part). My problem is I have a very large form which is nested( I can't change it). Large form in terms of input fields which need to be saved in data approx 100 fields. new field may open it depends on users selected options.

我对使用ajax在数据库中保存数据有疑问。(我能做这部分)。我的问题是,我有一个非常大的表单,它是嵌套的(我不能改变它)。输入字段的大型形式,需要保存在大约100个字段的数据中。新字段可以打开它取决于用户选择的选项。

ex- suppose one question is like which game you play. In multi select drop down if he selects one game than next questions would be how frequently you play this game .on which day which time and many more. Each game may have different set of question.

假设有一个问题是你玩的是哪种游戏。在多选择下拉如果他选择了一个游戏而不是下一个问题是你多久玩一次这个游戏。在哪一天哪个时间和更多。每个游戏都可能有不同的问题。

Now my problem is how to save this data in database. Should I save it after user click submit or should I save it in between user is feeling data. so that he refresh the data it can have his data filled .

现在我的问题是如何将这些数据保存到数据库中。我应该在用户点击提交后保存它,还是应该将它保存在用户感觉数据之间。这样他就可以刷新数据,让数据被填满。

How frequently should I send Ajax request for saving data and how to get the data from the fields which are newly field and how I should save it in Rails. I know about update.attributes

我应该多频繁地发送Ajax请求来保存数据,以及如何从新字段的字段中获取数据,以及如何将数据保存到Rails中。我知道update.attributes

please help me or give some suggestions how should I do it.

请帮助我或者给我一些建议我该怎么做。

1 个解决方案

#1


2  

If you live edit or only on save has mostly user expierence questions.

如果您是live edit还是仅在save上,大多数都有用户过期问题。

But if you are frequently saving (like an autosave) and are worried about the size (100 normal columns might be OK anyway, allthough large text or blobs less so) then what you want to do fairly simply is only save the fields that have actually changed.

但是,如果您经常保存(比如自动保存),并且担心大小(100个正常的列可能是OK的,尽管大的文本或blob不那么大),那么您想要做的仅仅是保存那些实际发生了变化的字段。

There are many ways to implement this in JavaScript. You might just save each input when the user finishes editing it (e.g. the input loosing focus) or you might save based on a timer and track the changed fields since last save.

有很多方法可以在JavaScript中实现这一点。您可以在用户完成编辑时保存每个输入(例如,输入失去焦点),也可以基于计时器保存,跟踪上次保存后更改的字段。

Then have your JavaScript just include those fields in its AJAX request (PATCH migh be a good method to use). Rails should then only try to save the attributes on the object that you changed (via update_attributes, or save on the ActiveRecord). If you want to also optimise out the SELECT, use update or update_all on the class. e.g. ends up like:

然后让JavaScript将这些字段包含在AJAX请求中(补丁migh是一个很好的方法)。然后,Rails应该只尝试将属性保存在更改的对象上(通过update_attributes,或save on the ActiveRecord)。如果还想优化SELECT,请在类上使用update或update_all。如最终:

MyBigRecord.update(id, title: "My new title")

You can easily use the normal strong parameters here, which only includes those actually present in params.

您可以在这里轻松地使用普通的强参数,它只包括那些实际存在于params中的参数。

MyBigRecord.update(id, params.require(:my_big_record).permit(:title, :author, :etc))

If you need to deal with sub objects then you may need some special handling, but the idea is the same). A little bit of logic can also do the initial create on demand, allthough your JavaScript then recieve the id to use for future saves.

如果您需要处理子对象,那么您可能需要一些特殊的处理,但是想法是一样的)。一点点逻辑也可以根据需要进行初始创建,尽管JavaScript会接收到用于将来保存的id。

#1


2  

If you live edit or only on save has mostly user expierence questions.

如果您是live edit还是仅在save上,大多数都有用户过期问题。

But if you are frequently saving (like an autosave) and are worried about the size (100 normal columns might be OK anyway, allthough large text or blobs less so) then what you want to do fairly simply is only save the fields that have actually changed.

但是,如果您经常保存(比如自动保存),并且担心大小(100个正常的列可能是OK的,尽管大的文本或blob不那么大),那么您想要做的仅仅是保存那些实际发生了变化的字段。

There are many ways to implement this in JavaScript. You might just save each input when the user finishes editing it (e.g. the input loosing focus) or you might save based on a timer and track the changed fields since last save.

有很多方法可以在JavaScript中实现这一点。您可以在用户完成编辑时保存每个输入(例如,输入失去焦点),也可以基于计时器保存,跟踪上次保存后更改的字段。

Then have your JavaScript just include those fields in its AJAX request (PATCH migh be a good method to use). Rails should then only try to save the attributes on the object that you changed (via update_attributes, or save on the ActiveRecord). If you want to also optimise out the SELECT, use update or update_all on the class. e.g. ends up like:

然后让JavaScript将这些字段包含在AJAX请求中(补丁migh是一个很好的方法)。然后,Rails应该只尝试将属性保存在更改的对象上(通过update_attributes,或save on the ActiveRecord)。如果还想优化SELECT,请在类上使用update或update_all。如最终:

MyBigRecord.update(id, title: "My new title")

You can easily use the normal strong parameters here, which only includes those actually present in params.

您可以在这里轻松地使用普通的强参数,它只包括那些实际存在于params中的参数。

MyBigRecord.update(id, params.require(:my_big_record).permit(:title, :author, :etc))

If you need to deal with sub objects then you may need some special handling, but the idea is the same). A little bit of logic can also do the initial create on demand, allthough your JavaScript then recieve the id to use for future saves.

如果您需要处理子对象,那么您可能需要一些特殊的处理,但是想法是一样的)。一点点逻辑也可以根据需要进行初始创建,尽管JavaScript会接收到用于将来保存的id。