# Load file into text field?

**URL:** <https://www.ruby-forum.com/t/load-file-into-text-field/84324>\
**Category:** Rails\
**Created:** [February 3, 2007, 4:14am UTC](https://www.ruby-forum.com/t/load-file-into-text-field/84324 "2007-02-03T04:14:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [February 3, 2007, 4:14am UTC](https://www.ruby-forum.com/t/load-file-into-text-field/84324/1 "2007-02-03T04:14:01Z")

</div>

Does anyone know if it is possible to load text from file into a text  
field, and using ajax to upload the field so that when the file has  
changed, the field will be updated automatically?

Thanks.

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [February 3, 2007, 10:43am UTC](https://www.ruby-forum.com/t/load-file-into-text-field/84324/2 "2007-02-03T10:43:16Z")

</div>

On 2/3/07, Wai T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does anyone know if it is possible to load text from file into a text  
> field, and using ajax to upload the field so that when the file has  
> changed, the field will be updated automatically?

The first bit is easy. “@text = File.read(filename)” in the  
controller, and use it somewhere in your view to show it.

The second bit can be done in a couple of ways.

The easier one is to have the client poll the server for changes, and  
send the new text when it has. Take a look at  
periodically\_call\_remote with the :update option. If your file is  
large, you’ll want to have the client send the timestamp of the most  
recent version of the file it has received and check it to avoid  
sending the whole file each time.

The harder way is to push the data out the client periodically. This  
is a well-known problem without a straightforward solution AFAIK.  
There’s a technique dubbed Comet, which is Flash-based. Or you could  
try keeping the HTTP connection open somehow. I think this has been  
discussed here recently; you could try searching the archives if this  
interests you.

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [February 4, 2007, 2:49am UTC](https://www.ruby-forum.com/t/load-file-into-text-field/84324/3 "2007-02-04T02:49:40Z")

</div>

Have a look here:

[http://juggernaut.rubyforge.org/](http://juggernaut.rubyforge.org/)

I’ve only just seen it myself, so not sure if it’s what you want. I’ll  
let  
you decide!

-Nathan

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [February 4, 2007, 1:00am UTC](https://www.ruby-forum.com/t/load-file-into-text-field/84324/4 "2007-02-04T01:00:28Z")

</div>

George O. wrote:

> The first bit is easy. “@text = File.read(filename)” in the  
> controller, and use it somewhere in your view to show it.
> 
> The second bit can be done in a couple of ways.
> 
> The easier one is to have the client poll the server for changes, and  
> send the new text when it has. Take a look at  
> periodically\_call\_remote with the :update option. If your file is  
> large, you’ll want to have the client send the timestamp of the most  
> recent version of the file it has received and check it to avoid  
> sending the whole file each time.
> 
> The harder way is to push the data out the client periodically. This  
> is a well-known problem without a straightforward solution AFAIK.  
> There’s a technique dubbed Comet, which is Flash-based. Or you could  
> try keeping the HTTP connection open somehow. I think this has been  
> discussed here recently; you could try searching the archives if this  
> interests you.

Thanks a lot. Polling the server for changes and send the entire new  
text isn’t very efficient if the file is large. Can ruby read from a  
fifo pipe? And is it possible to check for the pipe periodically for  
data?

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [February 4, 2007, 6:52am UTC](https://www.ruby-forum.com/t/load-file-into-text-field/84324/5 "2007-02-04T06:52:17Z")

</div>

unknown wrote:

> Have a look here:
> 
> [http://juggernaut.rubyforge.org/](http://juggernaut.rubyforge.org/)
> 
> I’ve only just seen it myself, so not sure if it’s what you want. I’ll  
> let  
> you decide!
> 
> -Nathan

It seems like doing File.read(“log.pipe”) would freeze the application

> \_\<

Juggernaut looks interesting, but isn’t it for creating socket  
connections?

---

<div class="post-metadata">

**Author:** ![sneptune](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sneptune](https://www.ruby-forum.com/u/sneptune)\
**Post date:** [February 4, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/load-file-into-text-field/84324/6 "2007-02-04T15:16:41Z")

</div>

On 2/4/07, Wai T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks a lot. Polling the server for changes and send the entire new  
> text isn’t very efficient if the file is large. Can ruby read from a  
> fifo pipe? And is it possible to check for the pipe periodically for  
> data?

That would not be very efficient, no. If the file is only being  
appended to, you could send the last file size instead, and only send  
the extra bytes.

In fact, if you’re after a “tail -f” sort of functionality, there was  
a thread on this recently: [http://rubyurl.com/9n4](http://rubyurl.com/9n4) .

Ruby can certainly read from fifos – check the IO docs. If you’re  
going to poll the server, I think you’d have an added complication in  
that each AJAX update request handler needs to access the same fifo  
somehow. This is quite a complicated road to be going down though  
IMHO.
