CSV filepath

Hi, I’m relatively new to rails so sorry if this is really basic. I’m
trying to import a CSV file, the contents of which will then be added to
a database table but I can’t get rails to see the file. Currently i have
this:

def import_csv
CSV.open(“students.csv”, ‘r’) do |row|
p row
end
end

When I run this i get an error saying “No such file or directory -
students.csv”. The csv file is in the “public” directory generated by
the rails app. what am I doing wrong? How can I get the path to the
public directory?

Thanks,
Henry

On Mon, 8 Jan 2007 15:44:40 +0100
Henry B. [email protected] wrote:

end

When I run this i get an error saying “No such file or directory -
students.csv”. The csv file is in the “public” directory generated by
the rails app. what am I doing wrong? How can I get the path to the
public directory?

The relative path starts at your Rails app root. Try
public/students.csv. Is this method in a model, controller,
some other script?

Using `p’ won’t work in a model or controller, but you can use
logger.info to write your own text to the application log if
you like.

Dom

Henry B. wrote:

When I run this i get an error saying “No such file or directory -
students.csv”. The csv file is in the “public” directory generated by
the rails app. what am I doing wrong? How can I get the path to the
public directory?

Try passing this:

File.join(File.expand_path(RAILS_ROOT), “/public/students.csv”)

to generate the full path to your file.

The File.expand_path() part may not be necessary on your system…
sometimes I need it and sometimes I don’t. :slight_smile:

Jeff
softiesonrails.com

Jeff C. wrote:

Try passing this:

File.join(File.expand_path(RAILS_ROOT), “/public/students.csv”)

That worked perfectly thanks! :smiley:

I didn’t need the File.expand_path this time although I will remember it
if I have future problems.
Thanks too Dom for your reply!

Henry

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs