3

I'm working with Ruby and I need to send a constant update status message to the user while a long-running task is being performed in the controller.

The method inserts some rows on the database. What I want is to show the user a message like this:

Upload in progress, X number of files inserted.

Currently I'm using a LoadMask jQuery plugin but it only shows a simple message, and I dont know how to send the number of rows inserted with out interrupting the process.

1
  • You'll probably have to represent long-running tasks in your database somehow, or at least store them in the session. Then add a new method to your controller that can return the message with the current status, and poll it via AJAX in your page. I doubt you can conveniently "push" updates to a view from a long-running controller. Commented Jul 9, 2012 at 22:26

2 Answers 2

1

You should consider moving this long running process to a background task, so you can immediately give feedback to the user. Once the background job has been submitted, depending on your background processing engine, you will be able to check the status of jobs. It's probably simple to keep track of the status yourself (depending on your task). Use either Redis or just in your database (create a separate table for this). Once the job has been submitted, you need to return the id of the job which you can use to poll for updates.

Create a new controller (or action) that you can use jQuery + Ajax and poll for changes. giving it a job_id (which your previous action returned)

Please also look at: Faye: http://faye.jcoglan.com/, http://railscasts.com/episodes/260-messaging-with-faye Goliath: http://postrank-labs.github.com/goliath/

http://railscasts.com/episodes/229-polling-for-changes

Delayed Job, Resque, etc.

Oh, If you are just looking for a simple file upload progress indicator, see this Rails, upload progress bar

You can try the streaming technique as well, lots of info on that here: Ruby on Rails 3: Streaming data through Rails to client

Sign up to request clarification or add additional context in comments.

1 Comment

I've decided to run a background task for this long-running task. For this I found very usefull Railscast#127 Rake in Background link Regards
0

This is a common need in Rails and using JQuery is the correct approach generally.

While the specifics of the code for the controller and view are dependent upon how you've built your application, here's a good place to start:

Railscast #136: jQuery : http://railscasts.com/episodes/136-jquery/

One thing you'll need to consider is how to periodically report from your method the number of rows inserted so far. Do you have a way to do that?

Review the railscast first -- it will give you an understanding of how to proceed at least to the next step.

5 Comments

This answer seems to lack actual content. "Look at this guide which is tangentially related to your question" isn't really enough.
You're question lacked actual code. I can't give a specific answer including code if you don't as well. When I was first learning how to do AJAX updates of a web page dynamically, that railscast helped me get started and I thought it may help you as well.
It's not my question. (An OP's comments are marked with a light blue background behind the username.) If a question lacks detail, it's better to ask for clarification instead of just posting whatever. While the material might be ultimately helpful, it doesn't address even the few specifics the OP did mention, and thus is not really an answer to the question.
I've decided to run a background task for this long-running task. For this I found very usefull Railscast#127 Rake in Background link Regards
For background tasks, I'm currently using resque/redis -- it works well. See stackoverflow.com/questions/10475296/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.