1

I have a link on my control panel this link, call an action of my controller.

<%= link_to "Enviar Correos".html_safe, {:controller => "users", :action => "email_all_users"}, :method => "get", :html =>  {:style => "color:#FAA732;" } %>

the action:

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

everything works great, but it's taking a lot of time, so after click on the link I would like to change the link for a loading image and then (when the action finish), I would like to show another imagen (done!) and then show the link.

I've never work with ajax. So I need some help.

Thanks in advance.

Jean
  • 5,201
  • 11
  • 51
  • 87
  • What client side technology are you using? (As showing/hiding) the loading image will be controlled by the client (browser). You may want to have a look at this : http://rails-learning.blogspot.in/2011/11/ajax-pagination-in-rails-3.html – TJ- Jan 13 '13 at 15:49

2 Answers2

0

Generate loader image from Link

Ajax Image Link

you need to write javascript to show the image on clicking the link

and hide image on load complete.

May this question work for you

Link of answer

Community
  • 1
  • 1
Mohammad Sadiq Shaikh
  • 3,160
  • 9
  • 35
  • 54
0

You have to put in your link

:remote =>  true

to make an Ajax call.

Rails will go to "email_all_users". Here you will have to quit the "redirect_to" sentence.

Into your views you have to place a "JS" view file like "email_all_users.js.erb", here you could write the necesary javascript to show the info "Los correos fueron enviados".

To show the loading image with, for example jQuery, you can put something like this:

$('body').ajaxStart(function() {
    $('body>*:not(:hidden)').fadeTo(0, 0.5)
    $(this).append('<img id="ajax_loader" src="/images/loading.gif">')
});
$('body').ajaxComplete(function() {
    $('body>*:not(:hidden)').fadeTo(0, 1)
    $('#ajax_loader').remove()
});
j.avi
  • 53
  • 5