我正在嘗試爲用戶添加html文件到我的Heroku應用程序的非常簡單的方法。這些文件將被保存在./log中以供稍後渲染。我已經測試我的代碼在本地(在開發和生產),但是當我嘗試上傳文件在我的Heroku託管回購,我得到的內部服務器錯誤500Rails 3 Heroku應用程序,FileUtils錯誤服務器端
控制器 upload.rb:
class UploadController < ApplicationController
def index
render :file => 'upload/uploadfile.haml'
end
def uploadFile
file_param = params[:upload][:datafile]
post = DataFile.save(file_param)
render :text => "File has been uploaded successfully"
end
end
模型 data_file.rb:
class DataFile < ActiveRecord::Base
def self.save(upload)
# Changed Default Destination: [__RAILS_DIR__/log]
name = "./log/" + upload.original_filename
# can haz data directory?
require 'FileUtils'
FileUtils.mkdir_p(File.dirname(name))
File.open(name, "wb") { |f| f.write(upload.read) }
end
end
視圖 uploadfile.haml:
%h1 File Upload
= form_for :upload,:url=>{:action => 'uploadFile'},:html => { :multipart => true } do |f|
%p
%label{:for => "upload_file"} Select File
\:
\#{f.file_field 'datafile'}
= f.submit "Upload"
Heroku的日誌:
2012-08-07T14:13:20+00:00 app[web.1]: Started POST "/uploadFile" for 69.29.117.99 at 2012-08-07 14:13:20 +0000
2012-08-07T14:13:20+00:00 app[web.1]: Processing by UploadController#uploadFile as HTML
2012-08-07T14:13:20+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"1dAXkMulNR0d8S/l6QC8JnpSDtNBaHoyKJezgnheR10=", "upload"=>{"datafile"=>#>}, "commit"=>"Upload"}
2012-08-07T14:13:20+00:00 app[web.1]: Completed 500 Internal Server Error in 3ms
2012-08-07T14:13:20+00:00 app[web.1]:
2012-08-07T14:13:20+00:00 app[web.1]: LoadError (no such file to load -- FileUtils):
2012-08-07T14:13:20+00:00 app[web.1]: app/models/data_file.rb:7:in save'
2012-08-07T14:13:20+00:00 app[web.1]: app/controllers/upload_controller.rb:8:inuploadFile'
2012-08-07T14:13:20+00:00 app[web.1]:
2012-08-07T14:13:20+00:00 app[web.1]:
2012-08-07T14:13:20+00:00 app[web.1]: cache: [POST /uploadFile] invalidate, pass
的Heroku:http://upload-example.herokuapp.com/
github上:https://github.com/halterj1/upload
請沒有試圖說服我使用回形針或carrierwave,不回答我的問題。任何幫助將大大讚賞,在此先感謝傢伙!
看看這是否有幫助? http://stackoverflow.com/questions/3691746/heroku-file-upload-problem – GnrlBzik 2012-08-07 14:44:12
你讀過這個嗎? https://devcenter.heroku.com/articles/read-only-filesystem – GnrlBzik 2012-08-07 14:48:58
我重讀https:// devcenter。heroku.com/articles/read-only-filesystem,我想我可能誤解了./log 的用法我也在https://devcenter.heroku.com/articles/s3上閱讀,如果我理解正確,請使用Amazon S3不會允許我使用外部html部分。 因此,我現在在如何完成外部html部分上傳損失。 – JonahAaron 2012-08-07 15:21:58