Reprocess your paperclip objects under rails console

Image is the most frequent content you want to use for your Ruby on Rails application. I prefer paperclip gem that works very well with Imagemagick. Quick example on how you can generate images in your application (actually written in gem guide), but I'd like to add few things, that are not that obvious and personally I used to stuck somewhere in between.

Introduce paperclip gem to your rails application

Very first step is to add such gem in Gemfile:

        gem 'paperclip'

and run bundle command to install gem:

bundle install

Once done, generate migration using paperclip generator:

rails generate paperclip User avatar
rake db:migrate

Line first is paperclip helper that makes certain migration attaching few columns to your User model with avatar prefix (there are: avatar_file_name: string, avatar_content_type: string, avatar_file_size: integer, avatar_updated_at: datetime). All these are paperclip things to keep your avatar.

Setting your model to use paperclip with specific settings

Finally, when you have your model with attached paperclip migration, you need to say how exactly this model should use (i.e. image sizes for thumbnails, previews, large and original; file name). I really like then application keeps accurate structure even with avatars.

So let's do this quickly with the following setup:

Paperclip.interpolates :normalized_file_name do |attachment, style|

has_attached_file :avatar,
  :path        => ':rails_root/public/system/posts/:normalized_file_name-:style.:extension',
  :default_url => '/images/missing_:style.png',
  :url         => '/system/posts/:normalized_file_name-:style.:extension',
  :styles      => {
  :micro       => ['600x300'],
  :preview     => ['200x']

validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

# normalized file name for paperclip (avatar)

def normalized_file_name
  extension = File.extname(avatar_file_name).downcase

The above code tells my rails take ID of new object and its title. Set the file name with it's style (we have three styles here: micro, preview and the one not shown here original) and extension at the end.

The form for image upload

= form_for [:admin, @post], :html => { :multipart => true } do |p|
        span Avatar
      = p.file_field :avatar, :title => "Select file"
        input class="file-path validate" type="text"
      - if @post.avatar?
        =image_tag @post.avatar.url(:preview), :class => 'img-thumbnail'
        - else
        =image_tag '', :class => 'img-thumbnail'
      - if @post.avatar?
      p = link_to 'Delete', admin_remove_post_avatar_path(@post), :remote => true, :data => {:confirm => "Sure?"}, :class => 'waves-effect waves-light btn red', :method => 'delete'

Don't forget to set routes.rb with remove_avatar route and posts controller where you do CRUD. Also permit avatar variable.

Reprocess your existing images

Finally, when you have all these above set and can add your images, you may want to play with sizes and different other things in your paperclip configuration. Once you have applied changes, you can reprocess your images in Rails console.

Product.all.each{ |p| p.image.reprocess! }

In short, this is about:
#rails console

Start discussion:
Related articles: