Problem loading Heroku file

I had a problem loading a CSV file into Heroku and processing it. It works fine in my local environment. Just make sure that I don’t need to save the file to Heroku, just refer to it at the time of the request to convert it to a string for processing and import into the database.

What I want to do:

  • Download CSV File
  • Remove the header block, depending on which network the report is from
  • Read the CSV data in the database. This step works just fine.

Controller Code:

  def create
    @account = Account.find(params[:report][:account_id])
    @file = params[:report][:file].read
    # logger.info file.inspect
    case @account.provider
    when "Microsoft AdCenter" then @file.gsub!(/\A(.*)\n\n/im, "")
    when "Google AdWords" then @file.gsub!(/\A(.*)\n/i, "")
    else
      raise "Invalid PPC report format"
    end
  end

Here's the stack trace:

Processing ImportController#create (for XX.182.6.XXX at 2010-09-11 09:19:01) [POST]
  Parameters: {"commit"=>"Upload", "action"=>"create", "authenticity_token"=>"XXXXXwoFpvRO3vN8XVXRDg8rikFsj2TFTW7mrcTgg=", "controller"=>"import", "report"=>{"account_id"=>"1", "file"=>#<File:/home/slugs/126077_0657264_9a92/mnt/tmp/RackMultipart.9845.0>}}

NoMethodError (private method `gsub!' called for #<Tempfile:0x2b8ccb63ece0>):
  /usr/local/lib/ruby/1.8/delegate.rb:270:in `method_missing'
  app/controllers/import_controller.rb:15:in `create'
  warden (0.10.7) lib/warden/manager.rb:35:in `call'
  warden (0.10.7) lib/warden/manager.rb:34:in `catch'
  warden (0.10.7) lib/warden/manager.rb:34:in `call'
  /home/heroku_rack/lib/static_assets.rb:9:in `call'
  /home/heroku_rack/lib/last_access.rb:25:in `call'
  /home/heroku_rack/lib/date_header.rb:14:in `call'
  thin (1.0.1) lib/thin/connection.rb:80:in `pre_process'
  thin (1.0.1) lib/thin/connection.rb:78:in `catch'
  thin (1.0.1) lib/thin/connection.rb:78:in `pre_process'
  thin (1.0.1) lib/thin/connection.rb:57:in `process'
  thin (1.0.1) lib/thin/connection.rb:42:in `receive_data'
  eventmachine (0.12.6) lib/eventmachine.rb:240:in `run_machine'
  eventmachine (0.12.6) lib/eventmachine.rb:240:in `run'
  thin (1.0.1) lib/thin/backends/base.rb:57:in `start'
  thin (1.0.1) lib/thin/server.rb:150:in `start'
  thin (1.0.1) lib/thin/controllers/controller.rb:80:in `start'
  thin (1.0.1) lib/thin/runner.rb:173:in `send'
  thin (1.0.1) lib/thin/runner.rb:173:in `run_command'
  thin (1.0.1) lib/thin/runner.rb:139:in `run!'
  thin (1.0.1) bin/thin:6
  /usr/local/bin/thin:20:in `load'
  /usr/local/bin/thin:20

Rendering /disk1/home/slugs/126077_0657264_9a92/mnt/public/500.html (500 Internal Server Error)

Does anyone know why it works fine, but then creates this error on Heroku?

Thank!

+3
source share
5

Avishal, Rails 3:

@file = IO.read(params [: report].tempfile.path)

+7

, . , Heroku, Tempfile. tempfile , ( CSV):

@file = IO.read(params[:report][:file].path)

text/csv, , , , S3, Joost.

+4

. , , csv, , . :

file = IO.read(params[:file].tempfile.path)
    FasterCSV.new(file, :headers => true).each do |row|
         # my parsing logic
    end

, . , , heruko, :

POST "/projects/1/upload_pivotal_csv" 122.172.25.106 2012-03-23 ​​07:45:59 +0000 2012-03-23T07: 46: 00 + 00: 00 [web.1]: 2012-03-23T07: 46: 00 + 00: 00 [web.1]: NotImplementedError (, CSV- Ruby 1.9). FasterCSV Ruby 1.9 m17n.): 2012-03-23T07: 46: 00 + 00: 00 [web.1]: //projects_controller.rb: 17: `upload_pivotal_csv ' 2012-03-23T07: 46: 00 + 00: 00 [web.1]: 2012-03-23T07: 46: 00 + 00: 00 [web.1]: 2012-03-23T07: 46: 00 + 00: 00 heroku [router]: POST castletrack.herokuapp.com/projects/1/upload_pivotal_csv dyno = web.1 queue = 0 wait = 0ms service = 783ms status = 500 bytes = 728 2012-03-23T07: 46: 00 + 00: 00 [web.1]: ProjectController # upload_pivotal_csv HTML 2012-03-23T07: 46: 00 + 00: 00 app [web.1]: : { "utf8" = > "βœ“", "authenticity_token" = > "sBmRWpGP3q9Hu7O2cMlmnGTByaTXValxYHw5 + cFoSw0 =", "file" = > # > , "commit" = > "Import", "id" = > "1" } 2012-03-23T07: 46: 00 + 00: 00 [web.1]: 406

I'm sure this has something to do with the ruby ​​version. I use ruby ​​1.8.7 locally and the rails 3 app. I don’t know which version of Ruby exists on heruko. But I can say that I am using the ceder stack.

+1
source

Yeah. the file system is read-only. You can store your files on S3 .

-2
source

Source: https://habr.com/ru/post/1764305/


All Articles