Paperclip style not working with proc

I have a stream_type field on my form. When the form submits,
instance.stream_type is blank. To verify this, in my custom processor
(class ProcessAudio < Processor), I do puts options[:geometry].

  has_attached_file :media,
                    :styles => { :original => { |instance|
instance.stream_type } },
                    :url => '/assets/artists/:artist_id/
                    :path => ':rails_root/public/assets/
                    :processors => [:process_audio]

If I provide a fixed string. For example:

  has_attached_file :media,
                    :styles => { :original => '30' },
                    :url => '/assets/artists/:artist_id/
                    :path => ':rails_root/public/assets/
                    :processors => [:process_audio]

puts options[:geometry] = 30. Why does is it NOT work with proc?

What is wrong?

anyone?? Been on this for days without a solution.... thinking that I
may have to switch to another gem just to get this feature to work...

What are you trying to accomplish here? What do you want the form to send to Paperclip, or what do you want Paperclip to send to your model?


Your main problem ( and i apologise since i havent have time do make the demo i promise you) is that you are fixed into doing this thing with processors which make everything more ortodox but also a bit harder, i for example do every with filters in my models and i just pass commands to the shell or workling.

has_attached_file gets read when the class file is first read. It then sets up the various paperclip methods that do there stuff. When it does that, 'instance' is a new/blank record.

What you have below will work (if memory serves me right) if you save the record, reload it, and then reprocess it.

Something along those lines... it's been awhile since I ran into this.


Walter Lee, I am trying to pass a value (stream_type) that was in
submitted in my form to :styles using proc.

Radhames, even if I do all this in the model, without a processor, I
am still not able to get the value of stream_type, that was passed
when the form tried to submit.

So Philip, this means I would need to reprocess the file and delete
the original? This means I will be processing the file twice? Isn't
this a waste of resources if I only want one processed file that is
adherent to the stream_type?

I built a system in Rails 2.3.8 that accepted PDF uploads and needed to extract their text content using the venerable (read ancient) pdftotext command-line utility. I had to jump through the following hoops to make it work, and this might have some bearing on your solution:

   has_attached_file :pdf,:styles => { :text => { :fake => 'variable' } }, :processors => [:text]
   after_post_process :extract_text

   def extract_text
     file ="#{pdf.queued_for_write[:text].path}","r")
     plain_text = ""
     while (line = file.gets)
       plain_text << Iconv.conv('ASCII//IGNORE', 'UTF8', line)
     self.plain_text = plain_text

module Paperclip
   # Handles extracting plain text from PDF file attachments
   class Text < Processor

     attr_accessor :whiny

     # Creates a Text extract from PDF
     def make
       src = @file
       dst =[@basename, 'txt'].compact.join("."))
       command = <<-end_command
         "#{ File.expand_path(src.path) }"
         "#{ File.expand_path(dst.path) }"

         success ="/usr/bin/pdftotext -nopgbrk", command.gsub(/\s+/, " ")) "Processing #{src.path} to #{dst.path} in the text processor."
       rescue PaperclipCommandLineError
         raise PaperclipError, "There was an error processing the text for #{@basename}" if @whiny

Within the environs of Paperclip, you can write processors that do pretty much anything, and usually result in a new file saved as a new format in the attachments hierarchy. Once that process is done, you can access the result file and do other stuff with it. But I'm not sure if that answers your question at all, since you don't seem to be facing the same problem I was.

If your form posts a file to Paperclip, you don't get access to the file parts of that form submission directly in your controller, unless I'm missing something fundamental. But a processor can access them directly, at a very low level.


My form submits the file. The custom processor I made for Paperclip
uses FFMPEG. My custom processor expects a style to be set so that it
can process the audio based on that value. So for example, in my form,
I set stream_type to 30, my custom processor would process the audio
file to 30 seconds.

Walter, in your example. You already have a style set as a fixed
string (has_attached_file :pdf,:styles => { :text => { :fake =>
'variable' } },)

Your example is valid, because you are processing again after the file
is submitted. But, I don't want to reprocess. It would be a waste of
resources to do this twice. I want do it right the first time around.

Anyone have a solution to this problem?