KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Apr-28 13:57 UTC
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
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 }) -%>
<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
@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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-28 14:09 UTC
Re: Feed an .csv file up to server for importing data, then throw away?
On 28 Apr 2008, at 14:57, KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > > 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|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 Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Apr-29 16:03 UTC
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
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 }) -%>
<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
@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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Roger Pack
2008-Apr-29 17:19 UTC
Re: Feed an .csv file up to server for importing data, then throw away?
is there a destroy method for the file after you''ve used it, maybe? On Tue, Apr 29, 2008 at 10:03 AM, KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 }) -%> > <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 > @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 > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
webstermattb
2008-Jun-10 03:44 UTC
Re: Feed an .csv file up to server for importing data, then throw away?
I don''t know if you already figured this out but I think this is what
you want:
FasterCSV.parse(params[:file].read) do |row|
# print "Row =",row,"\n"
# work with data
end
On Apr 29, 11:03 am,
"KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I''ve written a routine allowing the client user to declare where
> their .csvimportfileresides andthensend it to theserverto
> import the many rows into an existing model table, andthenthrowitaway.
> 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 afile,
> that when it''s processed will be ''thrownaway''.
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 }) -%>
> <p><%= file_field_tag "file", :size => 75, :class
=> "csv-input" -%></
> p>
> <p><strong><label for="file">Fileto
Upload</label></strong></p>
> <p><%= submit_tag -%></p>
>
> 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
blowupbut some books say that afileobject is passed and thusFile
> 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 thisfileon MY OWN machine
but
> I''m having to HARD CODE thefilepath before the actualfilename that
> is delivered. You can see some ideas I''ve tried to make the
> params[:file] behave like aFILEobject and thus reveal its full path.
> I am grateful for any links or suggestions.
> Kathleen
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jun-10 12:51 UTC
Re: Feed an .csv file up to server for importing data, then throw away?
Hi Guys,
I posted these questions a long time ago and don''t see how they got in
today''s thread. Here''s an example of how I finally got it to
work. You
question Roger, about how I was going to "throw away" the file once
downloaded, I have no clue??
Kathleen
def import_asscounts
@accounts = Account.find(:all)
n = 0
unless params[:file].nil?
@account = Account.find(params[:account_id])
FasterCSV.new(params[:file]).each do |row|
if Asscount.find_by_account_id_and_code(@account, row[0] )
@account.asscounts.find(:first, :conditions => {:code =>
row[0] }).destroy
end
c = @account.asscounts.new
c.code = row[0]
c.description = row[1]
if c.save
n += 1
else
n += 1
d = Badboy.new
d.account_id = @account
d.converttable = ''Assccount''
d.keystring = row[0]
d.keyint = ''''
d.message = ''Asscount Record Failed to convert''
d.recno = n
d.save
end
end
flash.now[:notice]= "Asscount Import Successful #{n} new
records added to data base
for a total number of
#{@account.asscounts.size} records"
end
end
On Jun 9, 9:44 pm, webstermattb
<mattbwebs...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I don''t know if you already figured this out but I think this is
what
> you want:
>
> FasterCSV.parse(params[:file].read) do |row|
> # print "Row =",row,"\n"
> # work with data
> end
>
> On Apr 29, 11:03 am,
"KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> wrote:
>
>
>
> > I''ve written a routine allowing the client user to declare
where
> > their .csvimportfileresides andthensend it to theserverto
> > import the many rows into an existing model table, andthenthrowitaway.
> > 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 afile,
> > that when it''s processed will be
''thrownaway''. 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 }) -%>
> > <p><%= file_field_tag "file", :size => 75,
:class => "csv-input" -%></
> > p>
> > <p><strong><label for="file">Fileto
Upload</label></strong></p>
> > <p><%= submit_tag -%></p>
>
> > 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 blowupbut some books say that afileobject is passed and
thusFile
> > 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 thisfileon MY OWN
machine but
> > I''m having to HARD CODE thefilepath before the actualfilename
that
> > is delivered. You can see some ideas I''ve tried to make the
> > params[:file] behave like aFILEobject and thus reveal its full path.
> > I am grateful for any links or suggestions.
> > Kathleen- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---