I know this question has been asked several times but I could not find one post that perfectly solves my problem.
Background: I have a form that are currently being created/updated ajax-ly using Ruby on Rails and jQuery. I need to autosave the form content every 30 sec for every create/update action. Right now, I am focusing on making autosave create form to work.
I have something like below:
#controller method
def create
@report = Report.create(params)
end
#js
$(function() {
if ($("#report").length > 1) {
setTimeout(autoSaveForm, 30000);
}
});
function autoSaveForm() {
$.ajax({
type: "POST",
url: "report/create",
data: $("#report").serialize(),
dataType: "script",
success: function(data) {
console.log(data);
}
});
setTimeout(autoSaveForm, 30000);
}
Update: Upon closer inspection, $("#report").serialize() is not working. It is not capturing the form content. I am using cocoon to generate two-layered nested forms. I need to correctly serialize the form content.
Update: replace "#report" to "form" will do. Thanks a lot.