outputting to webpage the lines of a file or array as each line is processed.. eg ob_flush() for Ruby

Hello all,

I have some code that reads and works on each row of an array. Is it possible for Ruby on Rails to to display in a view/webpage each row as it is processed.

The idea is that I want the user of the app to see the progress that the app is making as the action runs. EG the user sees the following appear as the rails action runs..

"row 1 is processed...."

then as row 2 is processed

"row 1 is processed.... row 2 is processed...."

etc etc

I believe this is achieved in PHP by flushing the output buffer.. Can this be achieved with rails?

Thanks in advance!! :slight_smile: Jamie

Hi Jamie,

jamietssg wrote:

I have some code that reads and works on each row of an array. Is it possible for Ruby on Rails to display in a view/webpage each row as it is processed.

The idea is that I want the user of the app to see the progress that the app is making as the action runs. EG the user sees the following appear as the rails action runs..

"row 1 is processed...."

then as row 2 is processed

"row 1 is processed.... row 2 is processed...."

etc etc

Rails runs in a pretty strict request - response cycle. Each request fires up an instance of Rails which constructs and returns a response. One response per request. What you're describing can be accomplished in a couple of ways (at least), but unless the row processing takes a substantial amount of time (per row) you may not find either worth your while. Having said that ...

One way to handle this would be to utilize background processing via BackgrounDRb. You'd feed it the array and query it periodically from the client to get status.

Another way would be to set up the processing so that the client actually requests processing of one line, the server responds with status when that's complete, the response triggers some JS function which issues a new request to the server to process the next entry in the array, etc.

BackgrounDRb is, IMHO, probably the easier way to go from a programming perspective but, depending on your production support, you may not want / be able to support distributed processing.

HTH, Bill