Say there is a hash field that can have two possible value permutations, "foo" and "bar". How can I validate the hash value is one of the two?
class ValidateMe
validates :type => { :type => "foo" or :type => "bar" }
end
This results in an error. What is the proper way to handle this use case?
My actual case is using Paperclip to attach an image. I need to enforce the image is only .png or .jpg
class ValidateMe
validates_attachment :image,
presence => true,
:content_type => { :content_type => "image/png" }
end
Help with either code block is greatly appreciated. Thanks!
The best way to do this would be to pass an array of types to :content_type
class ValidateMe
validates_attachment :image,
presence => true,
:content_type => { :content_type => ['image/png', 'image/jpeg'] }
end
(My answer is based on code in Paperclip - Validate File Type but not Presence)
This can also be done using regular expressions. (Not as preferable)
class ValidateMe
validates_attachment :image,
presence => true,
:content_type => { :content_type => /^image\/(jpeg|png)$/ }
end
(source How can I restrict Paperclip to only accept images?)
I think Btuman's first answer would be considered canonical: The content_type key of content_type in validates_attachment can accept an array of valid content-types.
You can use :inclusion attribute for more see this http://guides.rubyonrails.org/active_record_validations_callbacks.html
Related
I have a field called visit_time with two distinct values. They are "AM" and "PM"
I check the presence of the visit_time by the following validation syntax.
validates_presence_of :visit_time,
message: "visit time is required"
Then I need to check the inclusion validation only if the visit_time is presence, for this I am using the Proc. But it is not working.
validates :visit_time,
:inclusion => { :in => [ 'AM', 'PM'],
:message => "%{value} is not a valid time" },
:if => Proc.new { |o| o.errors.empty? }
Let me know what's wrong on it. Is Proc is not working for inclusion ??? Thanks in advance.
If you want the inclusion validation to run only if it's present, you should change the Proc to this instead:
if: Proc.new { |o| o.visit_time.present? }
I'm using Paperclip in my project but some of my users are complaining that it's incorrectly rotating some images.
For some reasons I can't even imagine I figured it out that some files are with wrong exif orientation attributes. I was looking and I saw that paperclip calls ImageMagick by default using -auto-orient. I saw that the Thumbnail processor has an option to turn auto-orient on or off.
But I couldn't find a way to pass this to the Processor.
This is the code I have:
has_attached_file :photo,
styles: { :square => "400x400#" }
Does anyone now how to do that?
Thanks!
In the end I created a new processor which extends from the paperclip default Thumbnail processor to send the correct options.
class WithouAutoOrientProcessor < Paperclip::Thumbnail
def initialize(file, options = {}, attachment = nil)
options[:auto_orient] = false
super
end
end
And in the model I added
has_attached_file :photo,
styles: { :square => "400x400#" },
processors: [:WithouAutoOrientProcessor]
Although it is a valid option to add your own processor, this is how you pass the option to the processor:
In your styles hash replace your dimension strings with another hash
Put your old dimensions in the key geometry into this hash
The other key/value pairs are the options passed to the processor
You can of course pass auto_orient: false, too
Applying this to your model's code:
has_attached_file :photo,
styles: { square: { geometry: "400x400#", auto_orient: false } }
I have an uload field which is optional, it can be left empty. But when it is is used, I want to validate the size and content of the attachment. So I use this validation in the model:
validates_attachment :attachment, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }, size: { in: 0..500.kilobytes }
This works when there is an attachment, but fails when it is left empty. How can I make sure it only validates when there is an attached file?
The solutions mentioned here are not working unfortunately.
The link you provided is giving you what I would suggest - using the if: argument
--
if:
Using if: in your validation basically allows you to determine conditions on which the validator will fire. I see from the link, the guys are using if: :avatar_changed?
The problem you've likely encountered is you can either use a Proc or instance method to determine the condition; and as these guys are using a method on avatar (albeit an inbuilt one), it's not likely going to yield the result you want.
I would do this:
validates_attachment :attachment, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }, size: { in: 0..500.kilobytes }, if: Proc.new {|a| a.attachment.present? }
This basically determines if the attachment object is present, providing either true or false to the validation
try this:
has_attached_file :attachment, :styles => { :small => "200x200>" }
validates_attachment :attachment,
:size => { :in => 0..500.kiobytes },
:content_type => { :content_type => /^image\/(jpeg|png|gif|tiff)$/ }
its working on my app. except i have set a default attachment in case user chooses not to upload one.
I'm trying to insert the URL of the page into uploaded image.
I already have the code like this below but it doesn't work.
Is there something wrong in my model? How can I fix this?
My associations
User has_one :profile
Profile belongs_to :user
models/user.rb
before_save :text_to_insert?
def text_to_insert
nickname = self.profile.nickname
end
has_attached_file :user_avatar,
:styles => {
:thumb=> "100x100>",
:small => "400x400>" },
:convert_options => {
:small => '-fill white -undercolor "#00000080" -gravity South -annotate +0+5 " example.com/'+ nickname +' "' }
before saving , you are using text_to_insert? method which doesn't exist thatwhy it is returning false,so it fails to save .
It looks like typos ,try removing ? after :text_to_insert ie
before_save :text_to_insert
Please be sure that is valid self.profile.nickname
I have a Project model and I need to test if the billing_address_type is valid.
class Project < ActiveRecord::Base
validates :billing_address_type, :inclusion => { :in => %w(h o) }
def billing_address_types
options = {"Home" => "h", "Organisation" => "o"}
if person.present?
options.delete("Home") if person.address.blank?
options.delete("Organisation") if person.organisation.blank?
end
options
end
The validates line is wrong, however. I need to check for inclusion of the hash values returned by the method billing_address_types.
How can I check for the hash values only?
Thanks for any help...
You can pass lambda or a new Proc to the :in option which will be dynamically evaluated, and use the values method on the hash returned from billing_address_types to get the hash values only:
validates :billing_address_type, :inclusion => { :in => lambda { |a| a.class.billing_address_types.values } }
See the documentation for details.