小编典典

尝试弄清Ruby on Rails远程:真正的回调

ajax

所以我一直在研究如何设置它们,最后我得到了这段代码。

<script>

$('#reportform')
    .bind("ajax:success", function(data, status, xhr) {
        $('#reportalert').text('Done.');
    });
    .bind("ajax:error", function(xhr, status, error) {

        $('#reportalert').text('Failed.');

    });

</script>


<h2>Review Driver</h2>
<p>Fill out your review of the driver</p>

<div class="hero-unit form-signin" id="reportformdiv">

    <%= form_for(@report, html: { id: "reportform" }, remote: true, update: 
    { success: "response", failure: "error"} ) do |t| %>
<p id="reportalert"></p>
    <%= t.text_field  :plant_site,    placeholder: "Plant Site" %>

    <%= t.text_field  :route_number,  placeholder: "Route Number" %>

    <%= t.text_field  :driver_name,   placeholder: "Driver name if available" %>

    <%= t.date_select :date_recorded, html: { class: "input-block-level" } %>

    <%= t.text_field  :action,        placeholder: "Action taken" %>

    <%= t.text_area   :report_body,   placeholder: "What you witnessed",
                                     style: "height: 300px;",
                                     class: "input-block-level" %>

    <%= t.submit     "File Report",  class: "btn btn-primary btn-large" %>

    <% end %>

</div>

但是它不起作用,我也不知道为什么,我确定我做错了什么,我对RoR还是陌生的,我喜欢这样的事实:我可以以自己的形式声明此远程对象:弄清楚如何设置回调,我会很好的:)提前谢谢。


阅读 187

收藏
2020-07-26

共1个答案

小编典典

根据Rails的wiki,下面的代码应该可以工作:

<script>
  $(document).ready(function(){
    $('#reportform').on('ajax:success', function(e, data, status, xhr){
      $('#reportalert').text('Done.');
    }).on('ajax:error',function(e, xhr, status, error){
      $('#reportalert').text('Failed.');
    });
  });
</script>

类似的代码在Rails 3.2.14和jquery-rails 3.0.4中为我工作

希望能帮助到你。

2020-07-26