2013-01-31 4 views
0

남자 내 컨트롤러에 다음 작업을 수행하여 내 사용자에게 전자 메일을 보냅니다.rails 3.2 with ajax

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 "https://stackoverflow.com/users/#{current_user.id}", :flash => { :success => "Los correos fueron enviados" } 
end 

그리고 여기 당신이 링크를 클릭 한 후 볼 수 있듯이 나는이 내용

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

으로이 파일 email_all_users.js.erb을 만들

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

내보기에서 링크입니다 내가 링크를 숨기고, 내 부하 범위 (로드는 ID)에있는 이미지를 숨긴 다음 작업이 완료되면로드를 숨기고 내 전송 범위 (전송은 범위의 ID 임)를 표시하고 싶습니다.

내가 앱을 클릭 할 때 이메일을 보내지 만 링크를 숨기지 않고로드 및 보내기를 표시하지 않기 때문에 잘못된 행동을하고 있습니다. 당신의 도움이

솔루션에 미리

덕분에


내 자산 폴더에 내 email_all_users.js.erb를 이동하고 나는 이런 식으로 변경 : 해결

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

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

문제 .

아무튼 고마워요.

답변

0

이러한 ajax 호출은 작업에 대한 응답이 아닌 일반 js 파일로 이동해야합니다. 이 코드가 호출 될 때 ajax 요청은 이미 완료되었습니다.