# Strategy for importing temporary .csv file?

**URL:** <https://www.ruby-forum.com/t/strategy-for-importing-temporary-csv-file/136387>\
**Category:** Rails\
**Created:** [May 1, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/strategy-for-importing-temporary-csv-file/136387 "2008-05-01T16:10:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kathleen\_Kennedy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9de053/32.png) [@Kathleen\_Kennedy](https://www.ruby-forum.com/u/Kathleen_Kennedy)\
**Post date:** [May 1, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/strategy-for-importing-temporary-csv-file/136387/1 "2008-05-01T16:10:56Z")

</div>

For some reason, I’ve posted this question to this forum 4 times now  
and it never appears in the daily consolidated email I get daily?  
I’ve written a routine allowing the client user to declare where  
their .csv import file resides and then send it to the server to  
import the many rows into an existing model table, and then throw it  
away.  
I’ve imported many graphic files into my application using  
attachment\_fu and this was very straightforward as they were imported  
into an existing model object. Instead, I am trying to import a file,  
that when it’s processed will be ‘thrown away’. These examples are  
running ok, but I won’t be able to use this strategy in my production  
environment…  
Here’s my view code:

\<%= form\_tag(:url =\> {:action=\> “post” }, :html =\> { :multipart =\>  
true }) -%\>

\<%= file\_field\_tag "file", :size =\> 75, :class =\> "csv-input" -%\>

**File to Upload**

\<%= submit\_tag -%\>

Here’s my controller code:

class ImportController \< ApplicationController  
def aaccount\_import  
@owner = Owner.find(2)  
n = 0  
unless params[:file].nil?  
fullpath = File.expand\_path(params[:file]) # this doesn’t blow  
up but some books say that a file object is passed and thus File  
methods should apply?  
FasterCSV.foreach("#{RAILS\_ROOT}/convert/#{params[:file]}") do |  
row|

# FasterCSV.foreach(params[:file]) do |row| # cant seem to read

submitting machines full path info?

# FasterCSV.foreach(fullpath) do |row|

```
  c = @owner.accounts.new
  c.code = row[0]
  c.description = row[1]
  if c.save
    n += 1
  else
    n += 1
    d = Badboy.new
    d.message = 'Aaccount Record Failed to convert'
    d.recno = n
    d.save
  end
end

```

end  
end  
end

You’ll notice that I am able to import this file on MY OWN machine but  
I’m having to HARD CODE the file path before the actual file name that  
is delivered. You can see some ideas I’ve tried to make the  
params[:file] behave like a FILE object and thus reveal its full path.  
I am grateful for any links or suggestions.  
Kathleen

---

<div class="post-metadata">

**Author:** ![Kathleen\_Kennedy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9de053/32.png) [@Kathleen\_Kennedy](https://www.ruby-forum.com/u/Kathleen_Kennedy)\
**Post date:** [May 1, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/strategy-for-importing-temporary-csv-file/136387/2 "2008-05-01T16:28:32Z")

</div>

On 1 May 2008, at 15:09, Kathleen Kennedy wrote:

> row|
> 
> # FasterCSV.foreach(params[:file]) do |row| # cant seem to read
> 
> submitting machines full path info?

I replied the first time round I think. params[:file] is either a  
stringIO or a Tempfile, not a path to anything.  
FasterCSV.new.each do |row|  
…  
end

Fred
