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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mon, 8 Jan 2007 15:44:40 +0100 Henry Bourne <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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?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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Henry Bourne 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. :-) Jeff softiesonrails.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeff Cohen wrote:> Try passing this: > > File.join(File.expand_path(RAILS_ROOT), "/public/students.csv")That worked perfectly thanks! :D 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---