Register now and start sharing your code snippets.
-->

SEO optimized image URLs with the Paperclip Rails plugin

Ruby posted 3 months ago by christian

Create config/initializers/paperclip.rb:

   1  Paperclip::Attachment.interpolations[:permalink] = lambda do |attachment, style|
   2     attachment.instance.permalink
   3  end

In the model:

   1  has_attached_file :image, 
   2                      :path => ":rails_root/public/images:permalink/:style/:basename.:extension",
   3                      :url => "/images:permalink/:style/:basename.:extension",
   4                      :styles => { :large  => "250x360#",
   5                                   :medium => "150x230#",
   6                                   :small  => "110x150#" }

Instead of URLs like:

/images/products/249/large/temp.jpg

You can get a URL based on, for example, a permalink as in the example above. In my case I get URLs like this:

/images/games/nintendo-wii/large/super-mario-galaxy.jpg

Tagged paperclip, rails, ruby, plugin, seo, url