Dynamic clip attachment size (Rails)

Is there anyway validates_attachment_size, in addition to limiting the size of a dynamic file? Here is 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 tried this, but I keep getting the "Unknown Method" error message. Lambdas and Procs do not work either. Has anyone ever tried this? Thanks

+3
source share
3 answers

The porter type does not allow passing a function as a parameter of the size limit. Therefore, you probably need to write a special check:

  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
+6
source

A long shot ...

validates_attachment_size :document, :less_than => :get_current_file_size_limit

Normally, when passing a function, you should pass a character, not the actual function.

+2

:

validates_attachment_size :mp3, :less_than => 10.megabytes

mp3 .

See this post for more helpful clip tips: http://thewebfellas.com/blog/2008/11/2/goodbye-attachment_fu-hello-paperclip

0
source

Source: https://habr.com/ru/post/1743082/


All Articles