我正在尝试为用户添加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