If you are using paperclip and you upload a file containing spaces in the filename (ex windows users)  –  this can cause probs if your server is a unix box. Hence we would like to sanitize the filename of every attachment  – across all models that use paperclip

1. Create a file called paperclip_config.rb in config/initializers

2. Add the following lines to it

 

# Use the escaped_filename for all attachments
Paperclip::Attachment.default_options.merge!(  
  :path => ":rails_root/public/:attachment/:id/:style/:escaped_filename",
  :url =>  "/:attachment/:id/:style/:escaped_filename")

  
# ensure that escaped_filename for all attachments is actually escaped 
Paperclip.interpolates('escaped_filename') do |attachment, style|

  s = basename(attachment, style) 
  
  # Remove apostrophes so isn't changes to isnt
  s.gsub!(/'/, '')

  # Replace any non-letter or non-number character with a space
  s.gsub!(/[^A-Za-z0-9]+/, ' ')

  # Remove spaces from beginning and end of string
  s.strip!

  # Replace groups of spaces with single hyphen
  s.gsub!(/\ +/, '-')
  
  return s + "." + extension(attachment, style)
  
end