1

I've got a very simple file upload for a text file using paperclip.

What I want to do is to get the first line of the text file and turn it into a hash I can search against in a database.

I don't think it makes much sense to save the file, then retrieve it, create the hash, and then save it again.

I can't seem to figure out how to get the text of the attached file before saving.

my controller is fairly simple at the moment


def create 
     @upload = Upload.new(params[:upload])
     @upload.user_id=current_user.id
    #get the first line of the uploaded file

   if @upload.save
      redirect_to @upload, :notice =>'Successfully uploaded file."
      else 
      render :action => 'new'
      end
end

Going through the documentation, I've seen that paperclip has a to_tempfile, which I assume I can read into a string, but i can't seem to find anywhere that shows me how to do that.

pedalpete
  • 21,076
  • 45
  • 128
  • 239

1 Answers1

2

If params[:upload] is the file_field_tag in your form, then it's easier to bypass Paperclip and retrieve the first line of the file directly using Rails, just do this:

first_line = params[:upload].tempfile.readline
smathy
  • 26,283
  • 5
  • 48
  • 68
  • Jason, when you say 'bypass Paperclip', do you mean remove paperclip completely? I tried your solution (which seems to be exactly what I'm looking for), but I get undefined method 'tempfile' for # – pedalpete May 03 '11 at 22:05
  • Based solely on your error, try just: `params[:upload].readline` And by "bypass" I just meant not to use it for **this** - up to you whether you remove it, if this was all you were using it for then I'd say yes :) – smathy May 03 '11 at 22:34
  • Thanks Jason, I am using paperclip for more than just getting the first line, I do need to store the uploaded file, so I'll leave it there. But just in trying to get this to work, I tried the standard file_field_tag and then the code you provided with and without readline, unfortunately, I still get the same undefined method error. – pedalpete May 03 '11 at 23:33
  • ...and you went back and tried the params[:upload].tempfile.readline too? It works for me, I tried it before recommending it. You're not using Rails2 by any chance are you? – smathy May 03 '11 at 23:46
  • I'm using rails 3, I went back and tried it again with just tempfile, and with readline, but I get the undefined method 'tempfile' for {"file"=> "test_file.csv"}:ActiveSupport::HashWithIndifferentAccess – pedalpete May 04 '11 at 00:26
  • Ok, then `params[:upload]` is **not** your `file_field_tag` - you need to use: `params[:upload][:file]` instead, so: `first_line = params[:upload][:file].tempfile.readline` – smathy May 04 '11 at 16:01
  • after MANY MANY hours, I finally got this. the answer was just a bit off Jason, apparently I needed to use first_line=params[:upload].readline, without tempfile. Not sure why this is in my environment, and that is with paperclip. Once I got that working, I didn't try it again without paperclip. Thanks – pedalpete May 04 '11 at 16:11
  • That was precisely what I suggested in my first comment above :) Glad to hear you got it working. – smathy May 04 '11 at 17:14