我正在使用Carrierwave,Rails上载文件时出现以下错误。
在2018-08-06 10:46:43 +0530的192.168.1.101上启动POST“ / upload_client_input_file?project_id = 7” 由InputFilesController#upload_client_input_file作为 / 处理 参数:{“ file” =>#,@ original_filename =“ input.csv.part_1.1”,@ content_type =“ application / octet-stream”,@ headers =“ Content-Disposition:form-data; name = \”文件\“;文件名= \” input.csv.part_1.1 \“ \ r \ n内容类型:application / octet-stream \ r \ n”>,“ project_id” =>“ 7”} 在27毫秒内完成500个内部服务器错误(ActiveRecord:7.9毫秒)
TypeError(无法投射文件):
app / controllers / input_files_controller.rb:61:在“ upload_client_input_file”中
我的模型代码:
class InputFile < ApplicationRecord
belongs_to :project
mount_uploader :file, AvatarUploader
end
我的上传者代码:
class AvatarUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url(*args)
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process resize_to_fit: [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_whitelist
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
我的控制器代码:
def upload_client_input_file
params.permit!
project_id = params["project_id"]
tempfile = params["file"].tempfile
file = CSV.read(tempfile)
input_file_id = InputFile.find_or_create_by(batch_name: "Test", flag: true, project_id: project_id, file: File.open(tempfile,"r")).id
end