本文介绍了试图找出Ruby on Rails的遥控器:真正的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在谷歌上搜索关于如何设置他们,我结束了这个code到底。

So I've been googling on how to set them up, I ended up with this code in the end.

<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>

但它不工作,我不知道为什么,我敢肯定,我做错了什么,我是新来的回报率和我爱的事实,我可以宣布这个偏远的:真正的形式自身一旦我弄清楚如何设置回调,我会好到哪里去:)在此先感谢。

But it is not working and I have no idea why, I'm sure I've done something wrong, I'm new to RoR and I love the fact that I can declare this remote: true in the form its self, once I figure out how to set the callbacks I'll be good to go :) Thanks in advance.

推荐答案

根据 Rails的维基,在code波纹管应该工作:

According to Rails' wiki, the code bellow should work:

<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>

一个类似code Rails中3.2.14为我工作和jQuery护栏3.0.4

A similar code worked for me in Rails 3.2.14 and jquery-rails 3.0.4

希望它帮助。

这篇关于试图找出Ruby on Rails的遥控器:真正的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 23:46