如何更新我的div部分?

时间:2022-04-05 07:25:56

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。警报有效,但我似乎无法找到如何更新div comments_p

1 个解决方案

#1


0  

Hope the form is submitting to create method of Tasks controller. 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.

希望表单提交创建Tasks控制器的方法。由于请求是后端的js请求,因此只需在/ app / views / tasks /文件夹中创建一个名为create.js.erb的文件,如Unobtrusive Javascript Railscasts中所述,并编写您想要执行的代码在成功回应之后。

Get the updated tasks variable in tasks_controller.rb

获取tasks_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将被更新。

#1


0  

Hope the form is submitting to create method of Tasks controller. 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.

希望表单提交创建Tasks控制器的方法。由于请求是后端的js请求,因此只需在/ app / views / tasks /文件夹中创建一个名为create.js.erb的文件,如Unobtrusive Javascript Railscasts中所述,并编写您想要执行的代码在成功回应之后。

Get the updated tasks variable in tasks_controller.rb

获取tasks_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将被更新。