robzdc
robzdc

Reputation: 312

Paperclip skip style on first save

This is my code:

:styles => lambda { |attachment| attachment.instance.define_styles }

def define_styles 
    return_styles = Hash.new

    case self.imageable_type

    when "Admin::ProductDetail"
      return_styles[:thumb] = "70x60>"
      return_styles[:front] = "450x400>"
    else

    end

    return_styles
end

The problem is that when I first upload the image, it dont use the styles... only after I reupload the image, so it is only creating the default style, not the :thumb or :front in the first upload.

Upvotes: 1

Views: 309

Answers (2)

robzdc
robzdc

Reputation: 312

I added this code and it works now.

after_create :reprocess

def reprocess
  self.image.reprocess!
end

But I don't know if it's the right way to do it.

Upvotes: 1

usha
usha

Reputation: 29349

You can use the callback before_post_process

:styles => lambda { |attachment| attachment.instance.define_styles }

before_post_process :skip_on_create

def skip_on_create
  !new_record?
end

When this callback returns false, the post processing step will be halted

Documentation here

Upvotes: 1

Related Questions