我有一个评论模型,我希望人们能够保存草稿。它有一个布尔属性“draft”,允许评论被保存(但还没有出现)。我为这些评论创建了自动保存功能。如何更改表单在jQuery自动保存提交中提交的操作? (在Rails中)
评论表格当前运行如下:变量@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);
});
此代码设置了草案的输入是正确的,提交表单,然后删除,对草案的输入。这段代码很好用,因为它提交表单并将草稿保存到控制器。但是,每个提交都会保存一个新条目(即每隔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 %>
所以我想知道,我希望第一稿提交工作,因为它和在注释表创建一个条目。但是,我希望表单在随后的自动保存中更新该评论,并在该人员提交发布评论时将该评论保存为非草稿最终评论。在这些文件中有一处是我有概述说明我可以完成这个任务吗?
谢谢!
这几乎工作,但随后在POST到localhost,它运行一个无路由错误:ActionController :: RoutingError(没有路由匹配“/ posts/139”)。我不知道它为什么这样做,对我来说它应该与你写的代码一起工作。有任何想法吗? – geb2011
我的错误,请参阅我的编辑。 – bricker
是的,它修复了它!谢谢 – geb2011