I'm Jason, a web application developer in East Tennessee.

Paperclip URL and Path

I’m trying to configure my application so that it stores files in S3 by default when my application is running in the Production Rails environment, with local file storage and a customized file path for development and test environments. Here is my configuration.

Paperclip Defaults

You can view the default options by opening the Rails console and inspecting Paperclip::Attachment.default_options. I’m using Paperclip 3.5.1.

pry(main)> Paperclip::Attachment.default_options

=> {:convert_options=>{},
 :restricted_characters=>/[&amp;$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/,

Overriding Defaults

You can override the defaults in config/initializers/paperclip.rb using the following example which configures Paperclip to use S3 in production, with the ImageMagick path we use on the production server (running Ubuntu instead of MacOSX):

if Rails.env.production?
  # See
  Paperclip.options[:image_magick_path] = "/usr/bin/"

    :storage => :s3,
    :s3_credentials => "#{Rails.root}/config/s3.yml",
    :bucket => YAML.load_file("#{Rails.root}/config/s3.yml")[Rails.env]['bucket'],
    :url => ":s3_domain_url"

Here is my configuration for the local dev/test environments:

# Config for Non-production Environments
unless Rails.env.production?
    :url => "/system/:rails_env/:class/:attachment/:id_partition/:style/:filename",
    :path => ":rails_root/public:url"

Remember that the :path and :url have to align so that the file path and URL path match and the file is served by your web server. It’s best to modify the path only if the local filesystem prefix is different than the ‘public’ folder in the root of your Rails application directory.

comments powered by Disqus