系统不想在 ruby​​ on Rails 中写入 tmpfile?

发布于 2024-12-14 02:45:00 字数 2406 浏览 0 评论 0原文

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

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

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

 <% 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>

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

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

这只是我的简单示例,但在我的第二个视图中,一切正常,系统只显示第一个结果。但在我的第三个视图中,我收到一个错误,上面写着:undefined method `each' for nil:NilClass。所以@samples = nil。 有人知道我做错了什么吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖竹 2024-12-21 02:45:00

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

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

这将为您提供已上传文件的完整路径,以便您可以打开该文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文