自动保存在AJAX Rails的一种形式 [英] Autosaving a form in Rails with AJAX

查看:140
本文介绍了自动保存在AJAX Rails的一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动保存形式的帖子#新动作。每分钟左右,我想发表后自动保存#,然后我就检查first_or_create并保存/更新记录的帖子表所示。我的问题是,虽然,我不能再从形式访问post数据。我试图做到这一点,像这样:

I am trying to autosave a form for the Post#new action. Every minute or so, I want to POST to Post#autosave and then I'll check for first_or_create and save/updated the record in the Posts table. My problem though is, I can no longer access the POST params from the form. I am trying to do it like so:

$(function() {
  if ($("#new_post").length > 0) {
    setTimeout(autoSavePost, 60000); 
  }    
});

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

我有这样的路线:

I have this route:

post 'posts/autosave', as: :autosave_post_path

现在的问题是,服务器日志显示params哈希表,因为只有包含:动作和:控制器。我如何才能获得的东西会被发送的POST数据的一部分是等效的。

The problem is, the server log shows the params hash as only containing :action and :controller. How can I access the equivalent of what would have been sent as part of the POST data.

推荐答案

您需要通过数据参数以及通过的serialize~~V 方式:

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);
  }
});

这篇关于自动保存在AJAX Rails的一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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