[英]Why does file.original_filename doesn't work in Ruby on Rails?
i am implementing a few weeks now in Ruby. 我现在在Ruby中实施几个星期。 And what i want to do is read in a csv file, which i put then in a temporary file so i can access it in next views and manipulate the data.
我想做的是读取一个csv文件,然后将其放入一个临时文件中,这样我就可以在下一个视图中访问它并处理数据了。 I have implemented everything like examples on the internet, but i always get the error : undefined method `original_filename' for nil:NilClass
我已经在互联网上实现了所有类似于示例的东西,但是我总是收到错误:nil:NilClass的未定义方法“ original_filename”
My code in my view is: 我认为我的代码是:
<% form_for :dump, :url=>{:controller=>"project_importer", :action=>"match"}, :html => { :multipart => true } do |f| -%>
<table">
<tr>
<td>
<label for="dump_file">
Select a CSV File :
</label>
</td>
<td >
<%= f.file_field :file -%>
</td>
</tr>
</table>
and then in my controller i have: 然后在我的控制器中,我有:
file = params[:file]
@original_filename = file.original_filename
tmpfile = Tempfile.new("redmine_user_importer")
if tmpfile
tmpfile.write(file.read)
tmpfile.close
tmpfilename = File.basename(tmpfile.path)
if !$tmpfiles
$tmpfiles = Hash.new
end
$tmpfiles[tmpfilename] = tmpfile
else
flash[:error] = "Cannot save import file."
return
end
I really don't know what i am doing wrong, i hope someone can help me out. 我真的不知道我在做什么错,我希望有人能帮助我。 The file param is not empty.
文件参数不为空。
Thanks in advance! 提前致谢!
try 尝试
params[:dump][:file]
instead of 代替
params[:file]
Or print 或列印
<%= params.inspect %>
on the target page to inspect your params if this doesnt work. 在目标页面上以检查您的参数是否无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.