我在Ruby on Rails中实现,我只想做一些简单的事情,我只想读取一个csv文件,然后在视图中显示输出。我有一些代码,这似乎对我好,但我总是得到errror:不能将它视为档案转换成String 这是我的控制器:Ruby on Rails,阅读和显示csv文件不起作用?
def match
file = params[:file]
@original_filename = file.original_filename
tmpfile = Tempfile.new("redmine_project_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
session[:importer_tmpfile] = tmpfilename
sample_count = 5
i = 0
@samples = []
FasterCSV.open(file, "r") do |row|
@samples[i] = row
i += 1
if i > sample_count
break
end
end
和我的观点就是:
<% form_tag({:action => 'result'}, {:multipart => true}) do %>
<table>
<% @samples.each do |sample| %>
<tr>
<td>sample</td>
</tr>
<% end %>
</table>
有人能帮助我吗? 格尔茨
我做这个,我没有错误了。但我仍然没有任何输出。就像@samples是空的一样,这是怎么回事? – Ruben