Validate attachment presence using paperclip
Make sure you call the methods in the following order and not vice versa:
has_attached_file :image
validates_attachment_presence :image
Validation with condition works fine, too:
validates_attachment_presence :image, :if => :method
This is because validates_attachment_presence
is only available after saying has_attached_file
.
Related cards:
Deliver Paperclip attachments to authorized users only
When Paperclip attachments should only be downloadable for selected users, there are three ways to go.
The same applies to files in Carrierwave.
...
List of handy Ruby scripts to transcode different file types (often by using GPT)
It's 2024 and we have tools like ffmpeg, imagemagick and GPT readily available. With them, it's easy to convert texts, images, audio and video clips into each other.
For the everyday use without any parameter tweaking I'm using a collection of ti...
Delivering Carrierwave attachments to authorized users only
Preparation
To attach files to your records, you will need a new database column representing the filename of the file. To do this, add a new migration (rails g migration <name>
) with the following content:
class AddAttachmentToNotes...
Convert colorspace of images attached with Paperclip
has_attached_file(
:avatar,
:styles => { :large => "300x300", :small => "100x100" },
:convert_options => { all => "-colorspace RGB" }
)
validates_acceptance_of is skipped when the attribute is nil
validates_acceptance_of :terms
only works if terms
is set to a value. The validation is skipped silently when terms
is nil
.
While this behavior is u...
ActiveRecord: scoped `validates_uniqueness_of` allows one null value per scope
As you most likely know validates_uniqness_of :foreign_id
does not allow nil
values by default.
To allow nil
one has to set the :allow_nil => true
option.
Very unexpected scoping this validation will **not raise an error if foreign_id
...
Rails: Custom validator for "only one of these" (XOR) presence validation
For Rails models where only one of multiple attributes may be filled out at the same time, there is no built-in validation.
I've seen different solutions in the wild, each with different downsides:
- Private method referenced via
validate
: wor...
Always store your Paperclip attachments in a separate folder per environment
tl;dr: Always have your attachment path start with :rails_root/storage/#{Rails.env}#{ENV['RAILS_TEST_NUMBER']}/
.
The directory where you save your Paperclip attachments should not look like this:
storage/photos/1/...
stora...
Testing ActiveRecord validations with RSpec
Validations should be covered by a model's spec.
This card shows how to test an individual validation. This is preferrable to save an entire record and see whether it is invalid.
Recipe for testing any validation
In general any validation...
Using the Truemail gem to validate e-mail addresses
The Truemail gem (not to be confused with truemail.io) allows validating email addresses, e.g. when users enter them into a sign-up form. It runs inside your application and does not depend on an external...