Re: Handling File on Upload

spncrgr
2010-12-07T01:02:32Z

Thanks, Vladimir and David for your replies.

I did experiment with Paperclip without much success.  I look at the
post Vladimir linked to and that gave me the idea to create a custom
processor.  It didn''t work though.  No errors thrown, but no records
saved either :-/

I''m sure I did it wrong, but here''s the code:
http://www.pastie.org/1354053

I also experimented with taking Paperclip out of the picture and put
the code to create the records in the controller.  What I get now is
an error that says "invalid attribute".  The invalid attribute
provided is the first email address of the first line of the file.

Here''s that code:  http://www.pastie.org/1354053


Thanks again for any and all help.  This has been my largest hurdle to
overcome in Rails thus far.

-S

On Dec 6, 4:01 pm, David Kahn
<d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>
wrote:
> On Mon, Dec 6, 2010 at 1:47 PM, Vladimir Rybas
<vladimirry...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:
>
> > I recommend you to use Paperclip for files uploading.
> >http://railscasts.com/episodes/134-paperclip
>
> The only thing if I recall, that kept me from using paperclip for my
> purposes, is the file essentially attached to the model but does not allow
> for modification and re-saving the file --- am I mistaken? Unless things
> have changed recently, it would be super-cool if Paperclip would handle
file
> mods just as a mod to any attribute of the model... although I guess this
> gets away from the basic intent of Paperclip, which for what it does do
does
> it very nicely.
>
>
>
>
>
> > Besides it''s other awesome features, you want to use
Paperclip
> > Processors to play with a file after upload.
>
>
>http://mdeering.com/posts/018-paperclip-processors-doing-so-much-more...
>
> > To read file lines you can go for
>
> > File.readlines(@file.path).collect(&:chomp).each do |line|
> >  ... (create your models here)
> > end
>
> > to collect all lines.
> > `chomp` will cut end-of-line symbols, and @file it''s an
object which
> > is given by Paperclip.
> > Or if you have troubles with this one, here''s another option
>
> > IO.foreach("path/to/file.txt") do |line|
> >  ... (create your models here)
> > end
>
> > On Mon, Dec 6, 2010 at 5:54 AM, spncrgr
<thisbudsfory...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > > Hello,
>
> > > I need some help trying to figure out how to add the ability for
a
> > > user to upload a text file, and then have the application parse
that
> > > text file, and add each record as instance of a model object.
 I''ve
> > > found a lot of helpful articles out there, but I''m stuck
trying to
> > > bring it all together since I''m still very new to all of
this.
>
> > > Here''s my view code:
> > > # index.html.erb
> > > <% form_for :upload, :url => { :action => :upload },
:html =>
> > > { :multipart => true } do |f| %>
>
> > > Please select a file to upload:
> > >    <%= f.file_field :file %>
> > >    <%= f.submit ''Upload'', :disable_with
=> "Uploading..." %>
>
> > > <% end %>
>
> > > In my controller, I''ve been able to write something like
this to work
> > > with the data in my upload view:
>
> > > # mailing_lists_controller.rb
> > > def upload
> > >    file = params[:upload][:file]
> > >    @data = file.read
> > >  end
>
> > > so then the view looks like this:
> > > # upload.html.erb
> > > <% @data.split("\r\n").each do |row| %>
>
> > >    <%= row.split("\t").join(" | ") %>
>
> > > <% end %>
>
> > > So, where I''m stuck is translating that into saving each
row as a
> > > record in the database.  I''ve tried several different
things in the
> > > model (like creating a "process" method or using
"before_save"), but
> > > nothing has been successful thus far (probably because
I''m not
> > > implementing it correctly).
>
> > > Any help would be appreciated.
>
> > > Thanks,
> > > Spencer
>
> > > --
> > > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm.
> > > To unsubscribe from this group, send email to
> >
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib
e@googlegroups.com>
> > .
> > > For more options, visit this group at
> >http://groups.google.com/group/rubyonrails-talk?hl=en.
>
> > --
> > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> > To unsubscribe from this group, send email to
> >
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib
e@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/rubyonrails-talk?hl=en.

--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.