# Opening file content into a textarea

**URL:** <https://www.ruby-forum.com/t/opening-file-content-into-a-textarea/160143>\
**Category:** Rails\
**Created:** [February 12, 2009, 8:00am UTC](https://www.ruby-forum.com/t/opening-file-content-into-a-textarea/160143 "2009-02-12T08:00:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [February 12, 2009, 8:00am UTC](https://www.ruby-forum.com/t/opening-file-content-into-a-textarea/160143/1 "2009-02-12T08:00:39Z")

</div>

Hello,

Can somebody help me on how to open a file and show it on a text area,  
and be able to edit it and save it again.

Thanks,

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [February 12, 2009, 8:55am UTC](https://www.ruby-forum.com/t/opening-file-content-into-a-textarea/160143/2 "2009-02-12T08:55:48Z")

</div>

in ruby there is the class File:  
[http://www.ruby-doc.org/core/classes/File.html](http://www.ruby-doc.org/core/classes/File.html)

to get the contents of your file try something like:  
my\_file = File.new(path\_to\_file, ‘r’)  
@my\_file\_content = my\_file.read  
my\_file.close

now you can show your content inside the text area (you should know  
how to build a simple form for it).

after editing it, back in your controller write the updated contents  
to the file:  
updated\_content = params[:whatever]  
my\_file = File.new(path\_to\_file, ‘w’)  
my\_file.write(updated\_content)  
my\_file.close

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [February 12, 2009, 2:36pm UTC](https://www.ruby-forum.com/t/opening-file-content-into-a-textarea/160143/3 "2009-02-12T14:36:15Z")

</div>

Many thanks MaD, I’ll try it out.
