【问题标题】:rails 3.2 with ajax带有ajax的rails 3.2
【发布时间】:2013-01-15 15:26:12
【问题描述】:

伙计们,我在控制器上执行以下操作以向我的用户发送电子邮件。

def email_all_users
    User.all.each do |u|
    if !u.information.nil?
        if !u.information.business
            UserMailer.candidate_email(u).deliver
        else 
                UserMailer.business_email(u).deliver
        end
    else
            UserMailer.no_info_email(u).deliver
    end
  end
  redirect_to "/users/#{current_user.id}",  :flash => { :success => "Los correos fueron enviados" }
end

这是我认为的链接

<%= link_to "Enviar Correos".html_safe, {:controller => "users", :action => "email_all_users"}, :method => "get",  :id => "email_users", :html =>  {:style => "color:#FAA732;" }, :remote => true %>
<span id="loading" style="color:rgb(41, 160, 41);display:none;"><i class="icon-spinner icon-spin"></i> Enviando</span>
<span id="send" style="color:rgb(41, 160, 41);display:none;"><i class="icon-ok-circle"></i> Enviado!</span>

我用这个内容创建了这个文件 email_all_users.js.erb

$('#email_users').hide();
$('#email_users').ajaxStart(function() {
    $('#loading').show();
});
$('#email_users').ajaxComplete(function() {
   $('#loading')..hide();
   $('#send')..show();
});

正如您在单击链接后看到的那样,我想隐藏链接并显示我在加载范围内的图像(加载是 id),然后当操作完成时,我想隐藏加载并显示我的发送span(send 是 span 的 id)。

我做错了什么,因为当我点击应用程序发送电子邮件,但不隐藏链接,不显示加载和发送。

提前感谢您的帮助

解决方案


我将 email_all_users.js.erb 移动到我的 assets 文件夹中,并进行了如下更改:

$(document).ajaxStart(function() {
    $('#email_users').hide();
    $('#loading').show();
});

$(document).ajaxComplete(function() {
    $('#loading').hide();
    $('#send').show();
});

问题解决。

还是谢谢。

【问题讨论】:

    标签: ruby-on-rails ajax jquery ruby-on-rails-3.2


    【解决方案1】:

    那些 ajax 调用需要进入常规 js 文件,而不是作为对操作的响应。当此代码被调用时,ajax 请求已经完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多