Javascript RubyonRails使用AJAX保存
在我的Rails应用程序中,我正在寻找一种方法来保存一篇文章,并给出一个通知,上面写着“已保存”。而不重定向到任何地方 我可以在控制器中执行此操作,还是必须使用Ajax?如果我必须使用Ajax,有没有一种简单的方法来实现它 以下是我的控制器中的创建和更新操作:Javascript RubyonRails使用AJAX保存,javascript,ruby-on-rails,ruby,ajax,Javascript,Ruby On Rails,Ruby,Ajax,在我的Rails应用程序中,我正在寻找一种方法来保存一篇文章,并给出一个通知,上面写着“已保存”。而不重定向到任何地方 我可以在控制器中执行此操作,还是必须使用Ajax?如果我必须使用Ajax,有没有一种简单的方法来实现它 以下是我的控制器中的创建和更新操作: def create @document = current_user.documents.build(params[:document]) if @document.save redirect_to @document
def create
@document = current_user.documents.build(params[:document])
if @document.save
redirect_to @document.edit, notice: 'Saved'
else
render action: "new"
end
end
def update
@document = current_user.documents.find_by_url_id(params[:id])
if @document.update_attributes(params[:document])
redirect_to @document, notice: 'Saved'
else
render action: "edit"
end
end
看起来您正试图将“新文档”表单替换为“编辑文档”表单,并显示一条警告,说明该表单已保存 老实说,最简单的方法可能是使用jQuery将整个表单替换为部分表单。这不是最简单的,有很多方法可以完成所有的Javascript客户端,但这会让你开始 假设您有一个名为
\u new.html.erb
的部分“新文档”表单,创建一个名为create.js.erb
的文件,并将其放入其中:
$edit_form = "<%= escape_javascript(render :partial => 'new', :locals => {document: @document}) %>"
$("#document_form_container").html($edit_form)
$edit_form=“'new',:locals=>{document:@document})%>”
$(“#文档_表单_容器”).html($edit_表单)
然后确保您的表单在
表单\u for
标记中有:remote=>true
。如果您不想创建新的控制器操作(您可能不应该),那么我建议您将创建和更新操作设置为如下所示:
def create
@document = current_user.documents.build(params[:document])
if @flag = @document.save
respond_to do |format|
format.html
format.js
end
else
render action: "new"
end
end
def update
@document = current_user.documents.find_by_url_id(params[:id])
if @flag = @document.update_attributes(params[:document])
respond_to do |format|
format.html
format.js
end
else
render action: "edit"
end
end
然后在app/views/documents/create.js.erb中:
var results_html;
var status;
<% if @flag %>
results_html = $('<%= j(render("document"))%>');
status = "success";
<% else %>
results_html = $('');
status = "failure";
<% end %>
$('destination').append(results_html); //assuming you're inserting a list item or some content besides the alert onto the page
alert(status); // replace this with your actual alert code
var results_html;
var status;
<% if @flag %>
results_html = $('<%= j(render("document"))%>');
status = "success";
<% else %>
results_html = $('');
status = "failure";
<% end %>
$('#<%= @document.id %>').replaceWith(results_html); //assuming you're replacing a list item or some content besides the alert onto the page. also, make sure you're searching for the correct element id
alert(status); // replace this with your actual alert code
var结果\u html;
var状态;
结果_html=$('');
status=“success”;
结果_html=$('');
status=“失败”;
$('destination').append(results_html)//假设您在页面上插入一个列表项或警报之外的一些内容
警报(状态);//将其替换为您的实际警报代码
在update.js.erb中:
var results_html;
var status;
<% if @flag %>
results_html = $('<%= j(render("document"))%>');
status = "success";
<% else %>
results_html = $('');
status = "failure";
<% end %>
$('destination').append(results_html); //assuming you're inserting a list item or some content besides the alert onto the page
alert(status); // replace this with your actual alert code
var results_html;
var status;
<% if @flag %>
results_html = $('<%= j(render("document"))%>');
status = "success";
<% else %>
results_html = $('');
status = "failure";
<% end %>
$('#<%= @document.id %>').replaceWith(results_html); //assuming you're replacing a list item or some content besides the alert onto the page. also, make sure you're searching for the correct element id
alert(status); // replace this with your actual alert code
var结果\u html;
var状态;
结果_html=$('');
status=“success”;
结果_html=$('');
status=“失败”;
$('#')。替换为(结果\u html)//假设您正在替换页面上警报之外的列表项或某些内容。另外,请确保您正在搜索正确的元素id
警报(状态);//将其替换为您的实际警报代码
希望这有帮助。关键是rails允许您为控制器操作上的不同访问方法定义不同的模板。当您发出AJAX请求时,默认情况下将获得js.erb视图,该视图将允许您通过返回服务器返回时将运行的javascript来操作当前页面。是这样吗?我有
\u form.html.erb
和new.html.erb
。我目前正在使用jQuery,所以最好使用它。我是否需要用我自己的ID替换#文档(表单)容器
?是,并在渲染中用表单
替换新的
:部分。控制器中的新操作?我该怎么做?抱歉,我是RailsNo的新手,您的部分名为_form.html.erb,因此您希望呈现它而不是新的render:partial=>'form',:locals=>{document:@document}
在_form.html.erb上添加远程true怎么样?