nil:NilClass 的未定义方法“original_filename”
undefined method `original_filename' for nil:NilClass
我正在尝试将文件上传到电子邮件 attachment.When 我尝试上传,但出现错误
"undefined method `original_filename' for nil:NilClass"我做错了什么?
我需要从控制器中捕获文件,并在 params[:file] 不存在时处理异常?
在我看来,有一个简单的上传文件标签
%strong Send Email with PDF attachment:
= file_field_tag :file
我需要从控制器中获取文件,
这是我的控制器..
@name = params[:file].original_filename
if params[:file].present?
directory = "public/data"
path = File.join(directory, @name)
uniq_name = (0...10).map {(65 + rand(26)).chr}.join
time_footprint = Time.now.to_formatted_s(:number)
File.open(path, "wb") do |file|
file.write(params[:file].read)
@uniq_path = File.join(directory, uniq_name + time_footprint + File.extname(file))
File.rename(file, @uniq_path)
end
else
flash[:notice] = "You are going to send email without the invoice"
redirect_to update_estimate_job_final_invoices_path(@ticket)
end
第一行是@name = params[:file].original_filename
。如果 params[:file]
不存在,那么对 original_filename
的调用将失败。将该行移动到 if 语句中:
if params[:file].present?
@name = params[:file].original_filename
directory = "public/data"
path = File.join(directory, @name)
...
else
...
end
我正在尝试将文件上传到电子邮件 attachment.When 我尝试上传,但出现错误
"undefined method `original_filename' for nil:NilClass"我做错了什么?
我需要从控制器中捕获文件,并在 params[:file] 不存在时处理异常?
在我看来,有一个简单的上传文件标签
%strong Send Email with PDF attachment:
= file_field_tag :file
我需要从控制器中获取文件, 这是我的控制器..
@name = params[:file].original_filename
if params[:file].present?
directory = "public/data"
path = File.join(directory, @name)
uniq_name = (0...10).map {(65 + rand(26)).chr}.join
time_footprint = Time.now.to_formatted_s(:number)
File.open(path, "wb") do |file|
file.write(params[:file].read)
@uniq_path = File.join(directory, uniq_name + time_footprint + File.extname(file))
File.rename(file, @uniq_path)
end
else
flash[:notice] = "You are going to send email without the invoice"
redirect_to update_estimate_job_final_invoices_path(@ticket)
end
第一行是@name = params[:file].original_filename
。如果 params[:file]
不存在,那么对 original_filename
的调用将失败。将该行移动到 if 语句中:
if params[:file].present?
@name = params[:file].original_filename
directory = "public/data"
path = File.join(directory, @name)
...
else
...
end