gpt4 book ai didi

ruby-on-rails - Heroku文件上传问题

转载 作者:数据小太阳 更新时间:2023-10-29 07:43:36 24 4
gpt4 key购买 nike

我在将 CSV 文件上传到 Heroku 并进行处理时遇到了问题。它在我的本地环境中运行良好。请明确一点,我不需要将文件保存在 Heroku 上,只需在请求期间访问它,以便将其转换为字符串以进行处理并导入到数据库中。

我想做的是:

  1. 上传 CSV 文件
  2. 删除标题 block ,具体取决于报告来自哪个网络
  3. 将 CSV 数据读入数据库。这一步工作正常。

Controller 代码:

  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

这是堆栈跟踪:

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)

有人知道为什么它在本地工作得很好,但在 Heroku 上却产生错误吗?

谢谢!

最佳答案

根据 Avishal 的回答,我将其与 Rails 3 一起使用:

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

关于ruby-on-rails - Heroku文件上传问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3691746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com