How to read and write files?

dare ruby wrote:

I have used code :

if File.exists? RAILS_ROOT + "/public/images/bannerlist/#{params[:id]}"

Firstly, start using Pathname. I always put this in environment.rb:

   require 'pathname'    RailsRoot = Pathname.new(RAILS_ROOT).expand_path

That allows this:

   if (RailsRoot + 'public/images/bannerlist' + params[:id]).exist?

> a = File.read("public/images/bannerlist/martin2.gif")

That .read() call neglects the RAILS_ROOT. Never guess what the current folder is; always use complete paths.

Next, you are essentially reading a gif into memory and barfing it out into a file. This is slow, fragile, and high-risk. Consider using FileUtils to symlink the banner.gif to the rotating banner in the banner list.