English 中文(简体)
阅读新写的T TempFile吗? IOerror在Rub对铁路的封闭流
原标题:Read data from newly written TempFile doesn t work? IOerror closed stream in Ruby on Rails

我在鲁比拉执行铁路项目,我有2个观点。 在我的第一观点中,一读在档案中,显示第一行(该行的罚款),然后,我认为(在控制人的方法中)撰写临时文件。 同样。 我要再次表明这一临时档案的内容。 但是,我总是有以下错误:

IOError in Project importerController#result

closed stream

这是我的控制人员守则,其背后是:

 tmpfile = Tempfile.new("tempfile")
    if tmpfile     
      tmpfile.write(@samples)
      tmpfile.close
      tmpfilename = File.basename(tmpfile.path)
      tmpfilenametest = tmpfilename
      if !$tmpfiles
        $tmpfiles = Hash.new
      end
      $tmpfiles[tmpfilename] = tmpfile
    else
      flash[:error] = "Cannot save import file."
      return
    end
    @path = tmpfile.path
    session[:importer_tmpfile] = tmpfilename
   end 

接着,我想用以下方式阅读这一文件:

 tmpfilename = session[:importer_tmpfile]
      i = 0
      sample_count = 5

      if tmpfilename
      tmpfile = $tmpfiles[tmpfilename]
      if tmpfile == nil
        flash[:error] = "Tijdelijk bestand bestaat niet!"
        return
      end
    end


    fields_map = params[:fields_map]
      attrs_map = fields_map.invert

      @parsed_tempfile=CSV::Reader.parse(tmpfile)
       @sampletemp = []
     @parsed_tempfile.each  do |row| 

          @sampletemp[i] = row  

        i = i+1  

     end

在这一观点中,我只是想表明这一点,即:

<ul>
<% @sampletemp.each do |x| %>
<li>
<%= x %>
</li>
<% end %>
</ul>

是否有任何人看一看错了什么? 提前感谢!

最佳回答

AFAIK, 一份模板文件,在你关闭之前只剩下——因此不再有效。 如果你想在你关闭之后实际上把数据保存在一纸上,那么,你需要使用真实的档案。

Edit; According to our discussion, I don t think you need a tempfile at all. You are just displaying parts of the file-data that the user has uploaded for you. You don t need a real file object for that (tempfile or otherwise).

用户上载一个“档案”,你可以把CSV(如你本人所做的那样)放在后面,然后你可以把它当作一系列的扼杀。 在“@csv_lines”等词中加以拯救,在你们看来,可以进入“@csv_lines”阵列。

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签