8

I'm trying to use the default_url in my application but I store the images using S3. I'm not sure what URL I need to put in there or if I have to create a fake image just to get everything up there.

Since my images always render through S3, I don't know if it would work if I just put in some default images in my public folder. It doesn't seem to be working now - I also only have one image in there, and I know it has to resize. I can manually put the resized images in there but I'm still not sure what URL to use with default. Please help :)

2 Answers 2

22

I use paperclip with S3 with default images in the public folder. It works fine. My default_url statement looks like this:

:default_url => '/images/:attachment/missing_:style.png'

which means that for my attachment named avatar, setup with the styles small and large, I must create and put these images in the public dir:

  • /images/avatar/missing_small.png
  • /images/avatar/missing_large.png

It's pretty well documented.

For your reference, or in the case the problem is really somewhere else, here is my full paperclip statement:

has_attached_file :avatar,
  :styles => { :small => '60x60#', :large => '300x300#' }, :default_style => :large,
  :storage => :s3,
  :default_url => '/images/:attachment/missing_:style.png',
  :path => "users/:id/avatar/:style.:extension",
  :bucket => "bucket name",
  :s3_credentials => {
    :access_key_id => "access key id",
    :secret_access_key => "secret access key"
  },
  :url => ":s3_alias_url", # These two are only required when you alias S3 - e.g. want to use cdn.example.com rather than s3.amazonaws.com
  :s3_host_alias => "my.aws.alias" 
5
  • did not work for me. all my attachments served from S3, but default_url through assets pipeline for some reason.
    – bershika
    Feb 7, 2014 at 2:15
  • I didn't need the full path. Apparently the default_url just looks in the images folder.
    – Jwan622
    Jan 28, 2015 at 21:06
  • Your server should serve static assets as well...otherwise, it is not going to work Feb 12, 2015 at 16:01
  • 2
    well this solution does not work if you are deploying on heroku since they does not allow images to be stored in the public folder
    – svelandiag
    Mar 2, 2015 at 4:02
  • 1
    @SsouLlesS, my solution for heroku is to manually specify the path to S3. Here is what mine looks like: :default_url => "https://s3.amazonaws.com/images.myapp/default/:style/default_image.jpg" I then manually uploaded the file to Amazon S3 in 3 different styles, in their accompanying folders.
    – BoomShadow
    Jun 16, 2015 at 21:05
4

try this out: :default_url => ActionController::Base.helpers.asset_path('missing.png')

1
  • This is the correct solution because if you need to modify your default image, it will automatically bust the cache of the browser and download the new, updated image.
    – Chloe
    May 11, 2017 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.