使用jQuery自动保存时,如何提交表单以“更新"表单?而不是“创建"第一次保存后? [英] When autosaving w/jQuery, how do I submit a form to "update" rather than "create" after the first save?

查看:169
本文介绍了使用jQuery自动保存时,如何提交表单以“更新"表单?而不是“创建"第一次保存后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题的后续处理:什么是RESTful的保存方式草稿?

我想知道,创建的提交始终提交到rails中的"create"操作.我该怎么做:

I'm wondering, the submissions that are created always submit to the "create" action in rails. How can I make:

  1. 第一次自动保存会创建一个条目
  2. 所有后续自动保存都会更新保存的条目
  3. 最终提交的内容会更新同一条目

有关信息,请首先参阅相关问题.另外,我在Post模型上执行此操作,在其中添加了可以为true或false的字段草稿".我的jQuery提交是:

For information, see the related question first. Also, I'm doing this on the Post model, where I added a field "draft" that can be true or false. My jQuery submission is:

 $(document).ready(function() {
    $('#new_post').append('<div class="temp"> </div>');
    setInterval(function() {

      { $('#new_post .temp').html('<input type="hidden" name="post[draft]" id="post_draft" value="true" />');
  draft = $('#draft');
}

      $('#post_form form[data-remote]').submit();

     $('#new_post .temp').html('');
      }, 1000*30); // 1000ms * 60s = 1m

 });

此文件创建一个隐藏字段草稿",提交表单,然后删除该字段.

This file creates a hidden field "draft", submits the form, and then deletes the field.

此外,另一个可能有用的文件是create.js.erb.表单提交给控制器后,将调用并运行该嵌入的ruby javascript文件.目前,我为文件提供了一个变量草稿",例如:

Also, another file that may be of use is create.js.erb. Once the form is submitted to the controller, this embedded ruby javascript file is called and run. Currently, I make a variable "draft" available to the file such that:

 <% if draft %>
      #maybe putting code here can change the form to submit to "update" action next time?
 <% else %>
      # code that executes if it's a final submission, updating tables etc..
 <% end %>

也许这个文件是完成我的任务的地方?

Maybe this file is a place to accomplish my task?

如果需要更多信息,请告诉我.

If there is more information required, let me know.

推荐答案

也许您可以尝试以下方法:从第一个ajax表单提交取回帖子ID后,您可以修改后续表单提交以提供帖子ID并进行更改从POST到UPDATE的表单方法.那应该是触发记录更新的一种轻松方式.

Maybe you can try this: After you get back the post id from the first ajax form submit, you can modify subsequent form submit to supply the post id and change the form method from POST to UPDATE. That should be a restful way of triggering record updates.

这篇关于使用jQuery自动保存时,如何提交表单以“更新"表单?而不是“创建"第一次保存后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆