ruby - Getting error when validating picture presence Rails 4 -
this gig model
class gig < activerecord::base has_attached_file :image, :styles => { :medium => "360x170>", :bigger => "650x459>" } validates_attachment_content_type :image, :content_type => /\aimage\/.*\z/ validate :image_size_validation def image_size_validation if image.size > 2.megabytes errors.add(:base, "image should less 2mb") end end end
everything works great,i can not upload picture greater 2mb wanted,and gives notice that(written me).
question: when save form(of new product),without selecting picture,it throws error saying
undefined method > nil:nilclass
. instead of saying user,that should upload picture,so form can saved.
gig controller create
def create @gig = current_user.gigs.build(gig_params) if @gig.save redirect_to @gig, notice: "gig created" else render "new" end end
that error thrown line:
if image.size > 2.megabytes
obviously, if there isn't image... won't have size.
how check nil there:
def image_size_validation return if image.blank? if image.size > 2.megabytes
Comments
Post a Comment