Paperclip validating when no attachment
Try the following code.
validate :image_present
def image_present
if image.present? && image_file_size < 2.megabytes
errors.add(:file_size, "file size must be between 0 and 2 megabytes.")
end
end
Here, the validation will work if there is image present in the model and will skip the validation if there is no image.
Validate attachment content type Paperclip in Rails 2 application
You are getting image must be jpg or png
when trying to save without image because if the image is not present which implies that the content type is also missing i.e. they are not jpg
or png
.
To accomplish your task, you can add a custom validator where first you will check if the image is present and if yes, only then you will check the :content_type
of the image.
Add these in your Product
Model:
validate :master_image_type_present
def master_image_type_present
if master_image.present? && !(['image/png', 'image/jpg', 'image/jpeg'].include? master_image_content_type)
errors.add(:content_type, "invalid content type")
end
end
And, your validation will work as expected.
Paperclip and failed validation - avoid reupload
I use the two-step solution with a separate model. Although it's possible to code and hack your way around the default behaviour, you could also validate on the client-side with JS.
Paperclip and Postgres
Alison R. has posted a comment to the solution, but let me write a complete solution.
Try this:
validates_attachment_size :avatar, :less_than => 1.megabytes,
:unless => Proc.new {|model| model.avatar }
Paperclip: specify attachment fields in post_process callbacks
According to Paperclip documentation, you should be able to use before_bar_post_process
callback and if you return false
the post processing for bar will be halted:
class Attachment < ActiveRecord::Base
has_attached_file :foo
has_attached_file :bar
before_bar_post_process check_bar_condition
def check_bar_condition
...
return false if #some_condition_fails
...
end
end
Rails Paperclip how to delete attachment?
First off, when you create a check_box in a form_for (which it looks like you are), then the form should by default send :image_delete as "1" if checked and "0" if unchecked. The method declaration looks like this:
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
Which shows that you can assign other values if you want to, but that is of course optional.
Secondly, the call to manually delete an attachment without deleting the model instance to which it is attached to is:
@page.image.destroy #Will remove the attachment and save the model
@page.image.clear #Will queue the attachment to be deleted
And to accomplish your way of deleting the images through a checkbox, perhaps add something like this to your Page model:
class Page < ActiveRecord::Base
has_attached_file :image
before_save :destroy_image?
def image_delete
@image_delete ||= "0"
end
def image_delete=(value)
@image_delete = value
end
private
def destroy_image?
self.image.clear if @image_delete == "1"
end
end
This way, when you create your form and add the :image_delete checkbox, it will load the default value "0" from the User instance. And if that field is checked then the controller will update the image_delete to "1" and when the User is saved, it will check if the image is to be deleted.
Related Topics
How to Beautify Xml Code in Rails Application
How to Create Temp Dir in Ruby
Does Begin . . . End While Denote a 'Block'
Why Is "❨╯°□°❩╯︵┻━┻" with Such an Encoding Used for a Method Name
Trying to Understand Use of Self.Method_Name VS. Classname.Method_Name in Ruby
How to Embed Dynamic Ruby Code to "Javascript" Section in Slim Templates
Using Activerecord Interface for Models Backed by External API in Ruby on Rails
Enumerator as an Infinite Generator in Ruby
Rails Pass Params/Arguments to Activerecord Callback Function
How to Set the Httponly Flag on a Cookie in Ruby on Rails
How to Enable Compression in Ruby on Rails
Rails 4 Strong Parameters Without Required Parameters
Require Lib in Rspec with Ruby 1.9.2 Brings "No Such File to Load"