系统不想在 ruby​​ on rails 中写入 tmpfile? [英] System doesn't want to write tmpfile in ruby on rails?

查看:51
本文介绍了系统不想在 ruby​​ on rails 中写入 tmpfile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Ruby on Rails 中实现,我想这样做,在我的第一个视图中,我浏览到一个文件(CSV 文件).我读了它并将它放在一个临时文件中.然后在我的第二个视图中,例如显示前 5 行.然后在我的第三个视图中,我想再次显示第一行.我的控制器是:

I am implementing in Ruby on Rails, and i want to do so, that in my first view, i browse to a file (CSV-file). which i read in and put it in a TempFile. Then in my second view i show for example the first 5 lines. And then in my third view I want to show the first lines again. My controller is:

class ProjectImporterController < ApplicationController
  unloadable
  def index

    end
  end

def match   
   file = params[:file]


   @parsed_file=CSV::Reader.parse(file)
  sample_count = 5  

  @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

    session[:importer_tmpfile] = tmpfilename

  i = 0
  @samples = []
     @parsed_file.each  do |row|

            if i != 0   
            @samples[i] = row
            end

            if i == 0
                @headers = row
            end


            if i >= sample_count 
                break
          end

        i = i+1 

     end
   end

   end

   def result
     tmpfilename = session[:importer_tmpfile]

      if tmpfilename
      tmpfile = $tmpfiles[tmpfilename]
      if tmpfile 
        flash[:error] = "Tempfile doesn't exist!"
        return
      end  


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

        if i != 0 
          @samples[i] = row
        end

        if i == 0
          @headers = row
        end


        if i >= sample_count 
          break
        end

        i = i+1

     end
   end
end

现在我的第一个视图的代码是:

Now my code for the first view is just:

 <% form_tag({:action => 'match'}, {:multipart => true}) do %>
    <table">
       <tr>
         <td>
          <label for="dump_file">
            Select a CSV File :
          </label>
         </td>
         <td >
           <%= file_field_tag 'file', :size => 60%></p>
         </td>
       </tr>

     </table>

第二个和第三个视图相同:

second and third view are the same:

<ul>
<% @samples.each do |a| %>
<li>
<%= a %>
</li>
<% end %>
</ul>

这只是一个简单的例子,但在我的第二个视图中,一切正常,系统只显示第一个结果.但是在我的第三个视图中,我收到一条错误消息:nil:NilClass 的未定义方法 `each'.所以@samples = nil.有人知道我做错了什么吗?

This is just i simple example, but in my second view, everything works and the system just show the first results. But in my third view, i get an error which says :undefined method `each' for nil:NilClass. So that @samples = nil. Somebody who knows what i am doing wrong?

谢谢

推荐答案

您只能获得文件名,而没有完整路径.要获取上传的临时文件的路径,请在处理上传表单的操作中使用它:

You only get the filename without the full path. To get the Path to the uploaded Tempfile use this in the action proceeding your upload form:

tempfile=params[:file].tempfile.to_path.to_s

这会为您提供上传文件的完整路径,以便您可以打开文件.

This gives you full path to the uploaded file so you can open the file.

这篇关于系统不想在 ruby​​ on rails 中写入 tmpfile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆