class ApplicationController < ActionController::Base
protect_from_forgery
def uploadFile(file)
require 'fileutils'
file=params[:file]
if !file.original_filename.empty?
@filename=getFileName(file.original_filename)
File.open("#{File.expand_path(RAILS_ROOT)}/public/uploads/#{filename}", "wb") do |f|
f.write(file.read)
end
return @filename
end
end
def getFileName(filename)
if !filename.nil?
Time.now.strftime("%Y_%m_%d_%H_%M_%S") + '_' + filename
end
end
def save
unless request.get?
if filename=uploadFile(params[:file]['file'])
render :text=>filename
end
end
end
end
def create
@photo = Photo.new(params[:photo])
@photo.img_url=uploadFile(params[:img_url])
@photo.save
flash[:notice]="新建成功"
redirect_to photos_url()
end
报错 undefined method `original_filename' for nil:NilClass