Mr. Hueywong,
I have a similar problem in that I'm trying to deliver a .csv file to
my application so that it can be processed (imported) and then
discarded. I posted this about 4 times now and don't seem to hit
paydirt so here goes again:
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. 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 discarded. These examples are running fine, 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 }) -%>
<p><%= file_field_tag "file", :size => 75, :class => "csv-input" -%></
p>
<p><strong><label for="file">File to Upload</label></strong></p>
<p><%= submit_tag -%></p>
Here's my controller code:
class ImportController < ApplicationController
def aaccount_import
n = 0
unless params[:file].nil?
FasterCSV.foreach("#{RAILS_ROOT}/convert/#{params[:file]}") do |
row| #here I have to 'hard code' the file location for my
development environment
c = Account.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
I am not importing the file in this routine but only processing it.
The 'file' param is really a string and it needs to be a file
'hanging' out on my server until it is processed. I am grateful for
any links or suggestions.
Kathleen