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:
<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
Hi Kathleen,
Your problem is in the line above - the syntax for form_tag is
different from form_for, so the :multipart => true attribute isn't
being passed into the form tag correctly. You want:
<%= form_tag({:action=> "post" }, {:multipart => true}) -%>
Matt,
When I try that I get the "Unknown action No action responded to post
". This works fine the way it is, but I only get a string passed. I am
lost when trying to unravel this?
Kathleen
FasterCSV.foreach("#{RAILS_ROOT}/convert/#{params[:file]}") do |
would COUGH if it got a true file object. Thus, the above was sending
a string which made FasterCSV happy.
Now that I am sending FasterCSV a file object, the line looks like
this;
FasterCSV.new(params[:file]).each do |row|
I am happy to share my CSV import code should anyone want to see it.
Thanks again,
Kathleen