M.Octavio
M.Octavio

Reputation: 1808

how to send data from controller to a view in ruby

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.

Upvotes: 3

Views: 526

Answers (2)

SMathew
SMathew

Reputation: 4003

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

Upvotes: 1

Kevin Bedell
Kevin Bedell

Reputation: 13414

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.

Upvotes: 0

Related Questions