简体   繁体   English

回形针(导轨)的动态附件尺寸

[英]Dynamic Attachment Size for Paperclip (Rails)

Is there anyway to have the validates_attachment_size except a dynamic file size limit? 除了动态文件大小限制之外,还有validates_attachment_size吗? Here's an example: 这是一个例子:

class Document < ActiveRecord::Base
   belongs_to :folder
   has_attached_file :document
   validates_attachment_size :document, :less_than => get_current_file_size_limit

   private

   def get_current_file_size_limit
     10.megabytes # This will dynamically change
   end
end

I've tried this but I keep getting an error saying "unknown method". 我试过这个,但我一直收到错误说“未知方法”。 Lambdas and Procs don't work either. Lambdas和Procs也不起作用。 Has anyone ever tried this? 有没有人试过这个? Thanks 谢谢

Paperclip doesn't allow to pass function as size limit parameter. Paperclip不允许将函数作为大小限制参数传递。 So you probably need to write custom validation: 所以你可能需要编写自定义验证:

  validate :validate_image_size

  def validate_image_size
    if document.file? && document.size > get_current_file_size_limit
      errors.add_to_base(" ... Your error message")
    end
  end

Long shot... 远射......

validates_attachment_size :document, :less_than => :get_current_file_size_limit

Usually when passing a function you have to pass the symbol and not the actual function. 通常在传递函数时,您必须传递符号而不是实际函数。

There is a built-in Paperclip validation now: 现在有一个内置的Paperclip验证:

validates_attachment_size :mp3, :less_than => 10.megabytes

Change mp3 to whatever your paperclipped file's name is. 将mp3更改为任何纸质文件夹的名称。

See this post for more helpful Paperclip tips: http://thewebfellas.com/blog/2008/11/2/goodbye-attachment_fu-hello-paperclip 有关更多有用的Paperclip提示,请参阅此帖子: http//thewebfellas.com/blog/2008/11/2/goodbye-attachment_fu-hello-paperclip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM