如何更改 jQuery 自动保存提交中表单提交的操作? (在轨道中)
我有一个评论模型,我希望人们能够保存草稿。它有一个布尔属性“草稿”,允许保存评论(但尚未显示)。我正在为这些评论创建自动保存功能。
当前的注释表单运行如下:变量@comment由控制器初始化为@comment = Comment.new。那么评论的形式是:
<%= form_for @comment, :remote => true do |f| %>
<%= f.text_area :title, :class => "inputform" %>
<%= f.text_area :content, :class =>"inputform" %>
<%= f.submit "Submit", :class => "button" %>
<% end %>
所以,正如我所说,我希望它自动保存。为了开始完成它,我编写了这个 autosave_comments.js 文件:
$(document).ready(function(){
setInterval(function() {
$('new_comment .temp').html('<input type="hidden" name="comment[draft]" id="comment_draft" value="true" />');
$('#comment_form form[data-remote]').submit();
$('new_comment .temp').html('');
}, 10000);
});
此代码将草稿的输入设置为 true,提交表单,然后删除草稿的该输入。该代码效果很好,因为它提交表单并将草稿保存到控制器。然而,每次提交都会保存一个新条目(即每 10 秒就有一条新评论作为草稿保存在数据库中),而不是更新第一个条目。
最后一点背景知识:当表单提交到评论控制器时,它会提交到创建操作:
def create
@comment = params[:comment]
if @post.save
if params[:draft]
flash.now[:notice] = "draft autosaved"
else
flash.now[:success] = "comment created"
end
else
#code to output errors
end
respond_to do |format|
format.html
format.js
end
end
然后引用 create.js.erb 文件:
<% post = user.posts.last %>
<% if post.draft == false %>
//code here deals with a true submission of a comment, to append tables etc.
<% else %>
//maybe some code here could alter the form on draft submission to make it update the same post next time?
<% end %>
所以我想知道,我希望第一份草稿提交能够作为它会执行此操作并在 Comments 表中创建一个条目。但随后我希望表单在后续自动保存时更新该评论,并在该人提交评论以供发布时将该评论保存为非草稿最终评论。在这些文件之一中是否有某个地方我概述了我可以完成此任务?
谢谢!
I have a Comment model that I want people to be able to save drafts for. It has a boolean attribute "draft" that allows the comment to be saved (but not show up yet). I'm creating an autosave function for these comments.
The Comment form current runs like this: the variable @comment is initialized by the controller as @comment = Comment.new. Then the form for the comment is:
<%= form_for @comment, :remote => true do |f| %>
<%= f.text_area :title, :class => "inputform" %>
<%= f.text_area :content, :class =>"inputform" %>
<%= f.submit "Submit", :class => "button" %>
<% end %>
So, as I said I want this to autosave. To start accomplishing it, I wrote this autosave_comments.js file:
$(document).ready(function(){
setInterval(function() {
$('new_comment .temp').html('<input type="hidden" name="comment[draft]" id="comment_draft" value="true" />');
$('#comment_form form[data-remote]').submit();
$('new_comment .temp').html('');
}, 10000);
});
This code sets an input for draft to be true, submits the form, and then removes that input for draft. This code works great, as it submits the form and saves a draft to the controller. HOWEVER, every submission saves a new entry (ie every 10s a new comment is saved in the database as a draft), rather than updating the first entry.
A final bit of background: when the form is submitted to the comments controller, it submits to the create action:
def create
@comment = params[:comment]
if @post.save
if params[:draft]
flash.now[:notice] = "draft autosaved"
else
flash.now[:success] = "comment created"
end
else
#code to output errors
end
respond_to do |format|
format.html
format.js
end
end
This then references a create.js.erb file:
<% post = user.posts.last %>
<% if post.draft == false %>
//code here deals with a true submission of a comment, to append tables etc.
<% else %>
//maybe some code here could alter the form on draft submission to make it update the same post next time?
<% end %>
SO I am wondering, I would like for the first draft submission to work as it does and create an entry in the Comments table. BUT then I want the form to update that comment on subsequent autosaves, and to save the comment as a non-draft final comment when the person submits the comment for posting. Is there someplace in one of these files that I have outline that I could accomplish this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
create.js.erb
中:只需确保您的评论表单的 ID 为
comment_form
,或者相应地更改 jQuery 对象。 Rails 应该负责剩下的事情。编辑 我忘记了您还需要像 Rails 那样伪造 PUT 请求。由于某些浏览器不支持 PUT 请求,rails 使用 POST,然后添加一个隐藏的表单字段:
因此只需在
create.js.erb
中生成该字段即可:In
create.js.erb
:Just make sure that your comment form has an ID of
comment_form
, or otherwise change the jQuery object accordingly. Rails should take care of the rest.edit I forgot that you'll need to also fake the PUT request like Rails does. Since some browsers don't support PUT requests, rails uses POST and then adds a hidden form field:
So just generate that in your
create.js.erb
: