Feed an .csv file up to server for importing data, then throw away?

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

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
      n += 1
      d = Badboy.new
      d.message      = 'Aaccount Record Failed to convert'
      d.recno        = n


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.

On 28 Apr 2008, at 14:57, [email protected] wrote:

 FasterCSV.foreach("#{RAILS_ROOT}/convert/#{params[:file]}") do |


params[:file] will either be a StringIO or an actual instance of File
(not a file path or anything like that)

FasterCSV.new(params[:file]).each … probably does what you want


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs