将表单中的自定义参数发送到自定义Paperclip处理器

时间:2013-12-04 17:53:46

标签: ruby-on-rails ruby-on-rails-4 paperclip

我有一个自定义Paperclip处理器,它使用FFMPEG从MP4中提取屏幕截图。这非常有效,但我希望用户能够在处理之前选择上传表单中视频截图的截取时间,这一点到目前为止还没有。

upload.rb

class Upload < ActiveRecord::Base

has_attached_file :uploaded_file, 
    styles: { 
        screenshot: { :processors => [:screenshot], :format => 'png' }
    }, 
    default_url: "/images/:style/missing.png"

default_scope order('created_at DESC')

def paperclip_screenshot_time
    # I'm attempting to pull in params[:screenshot_time] (set in the view)
    self.screenshot_time.to_s
end

如果我将paperclip_screenshot_time方法更改为:

,则上述操作无效
def paperclip_screenshot_time
     '5' 
end

工作正常。

以下是处理器的摘录,正如我所说的那样也很好:

screenshot.rb

module Paperclip
class Screenshot < Processor

    def initialize(file, options = {}, attachment = nil)
        super
        @file           = file
        @options        = options
        @instance       = attachment.instance
        @current_format = '.*'
        @basename       = File.basename(@file.path, @current_format)
        @whiny          = options[:whiny].nil? ? true : options[:whiny]
    end

    def target
        @attachment.instance
    end

    def make

        # Removed for brevity

        begin
            system('ffmpeg -i ' + @file.path + ' -ss 00:00:0' + target.paperclip_screenshot_time.to_s + ' -f image2 -vframes 1 ' + tmp_dir + '/' + tmp_filename)
        end

       # Removed for brevity

    end
end
end

......以及视图中的相关字段。

upload.jst.erb

<input name="upload[screenshot_time]" type="number" min="0" max="9" value="4" class="js-form-input" />

控制器中也允许使用该字段:

uploads_controller.rb

def upload_params
  params[:upload].permit(:uploaded_file, :title, :screenshot_time)
end

因此,重新迭代问题似乎是在paperclip_screenshot_time中使用upload[screenshot_time]中的值进行设置。我应该怎么做呢?

感激不尽的任何帮助!

1 个答案:

答案 0 :(得分:0)

事实证明问题是在控制器中设置上传参数的顺序。

因此,为了在处理器中访问titlescreenshot_time,他们必须在 uploaded_file之前。因此,将upload_params方法更改为:

def upload_params
   params[:upload].permit(:title, :screenshot_time, :uploaded_file)
end

一切顺利。