[英]How can I update my div partial?
my forms goes like this 我的表格是这样的
index.html.erb index.html.erb
<div id="comments_p">
<%= render (:partial => "comment", :collection => tasks.comments) %>
</div>
<% form_for :comment, :url => comment_task_path(tasks.id), :html => {:remote => true, :class => "comment_form"} do |f| -%>
<%= f.text_field :remark, :placeholder => "Add Comments", :rows => 2,
:style => "width: 834px; height: 40px;"%>
<%= f.submit "submit"%>
<% end -%>
<script type="text/javascript">
jQuery.fn.submitWithAjax = function() {
this.submit(function() {
var test = $.post(this.action, $(this).serialize(), null, "script");
$(".comment_form")[0].reset();
alert("Comment Submitted");
return false
})
return this;
};
</script>
application.js application.js
jQuery.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})
$(document).ready(function() {
$(".comment_form").submitWithAjax();
})
my problem is that how can i update my div. 我的问题是我如何更新我的股利。 the alert works but i cannot seem to find how to update the div comments_p
警报有效,但我似乎找不到如何更新div comments_p
Hope the form is submitting to create method of Tasks controller . 希望表格提交创建 Tasks 控制器的方法。 As the request is an js request in the back-end, so just create a file called create.js.erb in /app/views/tasks/ folder as explained in the Unobtrusive Javascript Railscasts and write the code what ever you want to execute after the sucessful response.
由于该请求是后端中的js请求,因此只需按照Unobtrusive Javascript Railscasts中的说明在/ app / views / tasks /文件夹中创建一个名为create.js.erb的文件,然后编写您想要执行的代码经过成功的回应。
Get the updated tasks variable in tasks_controller.rb 在task_controller.rb中获取更新的任务变量
def create
#...code for creating the comments for the task
@tasks = ... #updated record with the latest comments
end
then the response code in the create.js.erb file shoild be like 然后将create.js.erb文件中的响应代码压缩为
$("#comments_p").html('<%= render_partial :comment, :collection => @tasks.comments) %>');
the comments_p div will be updated after the successful completion of the request by the above code. 成功完成请求后,上述代码将更新comments_p div。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.