Rails 3:在提交按钮中:disable_with不起作用

时间:2013-01-09 05:23:15

标签: ruby-on-rails-3

在下面的代码中:disable_with在rails 3中不起作用。但它在rails 2.2中完全正常工作

此代码有什么问题?

<%= submit_tag "Save", :class => "buttons",
                       :disable_with => "Processing",
                       :id => 'save_btn' %>

3 个答案:

答案 0 :(得分:6)

从版本3.2.5开始,您不应使用:disable_with。

<%= submit_tag "Save", 'data-disable-with' => "Processing", :class => "buttons", :id => 'save_btn' %>

<%= submit_tag "Save", data: { disable_with: "Processing" }, :class => "buttons", :id => 'save_btn' %>

答案 1 :(得分:1)

检查原始html中的以下内容:

您可以在https://github.com/rails/jquery-ujs上阅读更多内容。不同导轨版本的不同安装。

答案 2 :(得分:0)

我在Safari 8.0.6和最新的rails-ujs上遇到了类似的问题。这是我解决这个问题的方法:

%button{ class: 'btn btn-action js-submit', data: { method: :put, 'href' => '...', disable_with: "<i class='icon icon-spinner icon-spin'></i>".html_safe }} Submit
$('.js-select-plan').click(function(event) {
  event.preventDefault();
  var $target     = $(this),
      href        = $target.data('href'),
      method      = $target.data('method'),
      formTarget  = $target.attr('target'),
      csrfToken   = $('meta[name=csrf-token]').attr('content'),
      csrfParam   = $('meta[name=csrf-param]').attr('content'),
      form        = $('<form method="post" action="' + href + '"></form>'),
      metaData    = '<input name="_method" value="' + method + '" type="hidden" />';

  if (csrfParam !== undefined && csrfToken !== undefined) {
    metaData += '<input name="' + csrfParam + '" value="' + csrfToken + '" type="hidden" />';
  }

  if (formTarget) {
    form.attr('target', formTarget);
  }

  // disable button/link element
  var contentMethod = $target.is('button') ? 'html' : 'val';
  var replacement   = $target.data('disable-with');
  $target.data('ujs:enable-with', $target[contentMethod]());
  if (replacement !== undefined) {
    $target[contentMethod](replacement);
  }
  $target.prop('disabled', true);

  form.hide().append(metaData).appendTo('body');
  setTimeout(function(){
    form.submit();
  }, 50);
});

这在所有浏览器中都像魅力一样。为了获得更好的灵活性,您可能希望将其包装在帮助器中。

相关问题