Forum: Ruby on Rails Opening file content into a textarea

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A1247f73598357f06388e863c41aa624?d=identicon&s=25 Elias Orozco (elioncho)
on 2009-02-12 08:00
(Received via mailing list)
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,
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-02-12 08:55
(Received via mailing list)
in ruby there is the class File:
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
A1247f73598357f06388e863c41aa624?d=identicon&s=25 Elias Orozco (elioncho)
on 2009-02-12 14:36
(Received via mailing list)
Many thanks MaD, I'll try it out.
This topic is locked and can not be replied to.