Lee
Lee

Reputation: 627

Show loading screen while performing task in Rails 3

If my 'create' action takes a while to load (due to doing an API call and then a calculation) what's the best way to show the user a 'loading screen' while this task is performed in the background?

Upvotes: 3

Views: 5567

Answers (1)

Icid
Icid

Reputation: 1464

Write some AJAX magic ;) Show the loading image when activated and hide it when the AJAX call is complete. And if you don't want to use AJAX, well, hmm, you could check out this:

http://yehudakatz.com/2010/09/07/automatic-flushing-the-rails-3-1-plan/

There's also a gem out there, that somewhat does that. But I don't have any experience with that. I would go for the AJAX method =)

EDIT:

Yeah, you should call the create action with AJAX in jQuery. You could do:

function create() {

    $('#loading_image').show();

    $.ajax({
     type: 'POST',
     url: /* URL to your create action: e.g. '/user/create/' */,
     data: $('form').serialize(),
     success: createSuccessHandler,
     error: createErrorHandler,
     complete: hideLoadingImage
    });

}

function createSuccessHandler(data) {

    alert("User created!")

}

function createErrorHandler(data) {

    alert("It failed, ffs!")

}

function hideLoadingImage() {

    $('#loading_image').hide()

}

Upvotes: 7

Related Questions