【问题标题】:Update button not doing anything in Rails Form更新按钮在 Rails 表单中没有做任何事情
【发布时间】:2017-03-11 00:35:34
【问题描述】:

我在 Rails 应用程序中设置了两种典型的表单,一种用于创建,另一种用于编辑/更新。我的创建表单运行良好,但由于某种原因,我的更新表单按钮没有做任何事情。单击时没有任何反应,好像按钮已被停用或其他东西。我的假设是 Javascript 发生了一些奇怪的事情,但我找不到任何会导致这种情况的东西。我还认为可能缺少一个关闭的 html 元素,但情况并非如此,因为我有一个 RubyMine 可以检测到这些。我已经坚持了好几个小时了!

索引页上的表格:

index.html.erb

<%= form_for(@suggested_invite, remote: true) do |f| %>
      <div class="modal fade" id="mynewsuggestedinvite" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <h4 class="modal-title" id="myModalLabel">Suggested Invite</h4>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal" id="mynewsuggestedinviteclose">Close</button>
              <%= submit_tag "Create", class: "btn btn-primary" %>
            </div>
          </div>
        </div>
      </div>
  <% end %>

_suggested_invite.html.erb

<%= form_for(suggested_invite, :method => :put, remote: true) do |f| %>
     <div class="modal fade" id="myupdatesuggestedinvite_<%= suggested_invite.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
       <div class="modal-dialog modal-lg">
         <div class="modal-content">
           <div class="modal-header">
             <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
             <h4 class="modal-title" id="myModalLabel">Edit</h4>
           </div>
           <div class="modal-footer">
             <button type="button" id="myupdatebutton_<%= suggested_invite.id %>" class="btn btn-default" data-dismiss="modal">Close</button>
             <%= submit_tag "Update", class: "btn btn-primary" %>
           </div>
         </div>
       </div>
     </div>
 <% end %>

dashboard.js.erb

$(document).ready(function(){
    $(".city").on("change keyup", function() {
        var city = $(this).val()
        $.getJSON("https://maps.googleapis.com/maps/api/geocode/json?address="+encodeURIComponent(city), function(val) {
            if(val.results.length) {
                var location = val.results[0].geometry.location
                $(".lat").val(location.lat)
                $(".lon").val(location.lng)
            }
        })
    })
    function readURL(input) {

        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('.blah').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $(".imgInp").change(function(){
        readURL(this);
    });

    $('.modal').on('shown.bs.modal', function ()
    {
        var input = $('#'+this.id + ' .city')[0];
        var autocomplete = new google.maps.places.Autocomplete(input);
        google.maps.event.addDomListener(window, 'load');
    });

    $('#mynewsuggestedinvite').on('hide.bs.modal', function () {
        $('#mynewsuggestedinvite .form-control').val('');
        $('#mynewsuggestedinvite .imgInp').val('');
        $('#mynewsuggestedinvite .blah').attr("src", "placeholder-image.png");
    });

    $(function () {
        $('input[type="file"]').change(function () {
            if ($(this).val() != "") {
                $(".card-image").val($(this).val());
            }else{
                $(this).css('color', 'transparent');
            }
        });
    });

});

index 和 _suggested_invite partial 共享同一个 javascript 文件。我知道 JS 文件运行良好,因为索引文件中的创建表单运行良好。我很迷茫,如果我能提供更多信息,请告诉我。

编辑

我添加了一个 onClick 事件处理程序,并在单击按钮时启动该事件。我现在比以往任何时候都更加困惑。

【问题讨论】:

  • 检查开发者工具的网络标签,remote: true 表示您正在使用 AJAX,ajax 调用可能会返回错误

标签: javascript jquery ruby-on-rails ruby forms


【解决方案1】:

将submit_tag 更改为f.submit

<%= f.submit "Update", class: "btn btn-primary" %>

希望有帮助!

【讨论】:

  • 还是不行。没有错误或任何东西,所以除了您提出的解决方案不起作用之外,我不知道还能说什么。
  • 我真的认为它与javascript或其他东西有关,但我不知道在哪里?
猜你喜欢
  • 2015-08-11
  • 1970-01-01
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多