System doesn't want to write tmpfile in ruby on rails?

﹥>﹥吖頭↗ 提交于 2019-12-08 05:57:36

问题


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>

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?

Thanks


回答1:


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.



来源:https://stackoverflow.com/questions/8008979/system-doesnt-want-to-write-tmpfile-in-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!