Autosaving a form in Rails with AJAX

China☆狼群 提交于 2019-12-02 22:59:48

You need to pass the data param as well, via serialize method:

$.ajax({
  type: "POST",
  url: "/posts/autosave",
  data: $("#new_post").serialize(),
  dataType: "script",
  success: function(data) {
    console.log(data);
  }
});

Take a look at the serialize() function: http://api.jquery.com/serialize/ : You can use it to create an array of data to pass to your controller as parameters.

So, I am Explaining the new Form Script

function autoSavePost() {
  $.ajax({
      type: "POST",
      url: '/quotes',
      data: $("#new_post").serialize(),
      dataType: "script",
  });
}

Send the Post Request to the Controller which just render the edit form after inserting the Object in DB, or better say after persisting the Record.

After the Edit Page is Rendered we wrote a Script there to just update the object after some Interval

function autoSavePost() {
    $.ajax({
      type: "PATCH",
      url: "/post/<%= post.id %>",
      data: $("#edit_post").serialize(),
      dataType: "script",
      success: function(data) {
        console.log("data")
      }
    });
    setTimeout(autoSavePost, 60000);
  }

So, this approach is quite helpful and its a fully Working Auto Save Form Feature and it can be implemented on any type of Object. Thanks !

The Complete Problem and the Solution link when and how I encountered this

Auto Save Form Feature is failing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!