2013-01-09 52 views
2

在这下面的代码:disable_with没有在轨道3工作,但它是在轨工作完美2.2的Rails 3:在提交按钮:disable_with不工作

什么是错误的代码?

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

您在Gemfile中包含了“jquery-rails”吗? –

+0

是的,我有jQuery的铁轨(2.1.4)宝石 –

+0

什么HTML生成的'submit_tag'?那个页面上包含了哪些JavaScript? –

回答

5

从版本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' %> 
+0

我正在使用rails 3.0。上面的两个都不行.. OMG!真的很烦! –

+0

对于3.0,你的语法已经死了。唯一可能的其他事情是与该类或id相关联的其他一些JavaScript干扰该操作。 –

+0

我注意到,在我的宝石文件'jquery-rails'被赞扬:( –

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); 
}); 

这就像在所有浏览器魅力。为了获得更好的灵活性,您可能需要将其封装在助手中。