views:

60

answers:

1

I am developing a small Ruby-on-rails application. I am using 'roo' gem to open an excel file. But rails throws an IO error while attempting to open the file. It says file does not exist. It works fine in irb. My development machine is windows. Here is my code

file ="#{RAILS_ROOT}/public/data/import.xls"
file.gsub!("\\","/")
workbook = Excel.new(file)

Any help is appreciated thanks,
Abhilash

+1  A: 

It would be worth using the File class here rather than creating the path and gsubbing file separators. For example:

file = File.join(RAILS_ROOT, 'public', 'data', 'import.xls')

I'm pretty sure you don't need to worry too much about using backslashes for file separators though in Windows (I've stopped developing on windows though so can't test).

You can then test whether ruby thinks the file exists by doing File.exists?(file) prior to doing anything roo-specific.

Also, are you running your rails app and console as different users? That might cause some permissions problems in one but not the other.

Shadwell