Luigi
Luigi

Reputation: 5603

Opening a csv file server side (heroku) in Rails

I have a local dev Rails environment in which I have this code in my controller:

  def csv_to_array(file)
    @comment_array = []
    CSV.foreach(file, :col_sep => ",", :headers => true) do |column|
      @comment_array << column['Phrase']
    end
    @comment_array
  end

csv_to_array('../hub/data/comment_file.csv')

This works fine in my local environment. It opens the csv file, and parses it into an array. The root rails directory here is hub.

However - When I push this app to heroku and try to run the same code server side, it errors out because it can't locate the file. How can I open a csv file server side? Did just the location change, or is there something else I'm missing?

Upvotes: 1

Views: 818

Answers (1)

gmalette
gmalette

Reputation: 2459

In your local environment, you're running the Ruby process from the Rails root directory. This is why you're able to locate the file using '../hub/data/comment_file.csv' (or probably just './data/comment_file.csv'

However, Heroku and many other hosts will run it from other locations.

You'll have to specify the full path of the file:

File.join(Rails.root, 'data/comment_file.csv')

Upvotes: 2

Related Questions