Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6779285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:22:02+00:00 2026-05-26T16:22:02+00:00

I am implementing in Ruby on Rails, and i want to do so, that

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:22:03+00:00Added an answer on May 26, 2026 at 4:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing in Ruby, and I just want to read in a CSV-file.
I am implementing the following problem in ruby. Here's the pattern that I want
I am implementing in Ruby on Rails, and I have a view, with all
I am implementing a JSON API using ruby on rails 3.1. Which tools/gems/plugings should
I am implementing in Ruby on Rails and I am trying to work with
I'm implementing an application in ruby on rails (although this is a minor detail)
I am implementing in Ruby on Rails, and i have 2 views. In my
A little background first. I am looking into the possibility of implementing Ruby's ActiveRecord
i am implementing a few weeks now in Ruby. And what i want to
I am currently implementing a blog-like site using Ruby on Rails. Each Post has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.