0

I'm trying to setup the default image for my user's avatars and running into the problem of it not showing up in my view(s). My user.rb file is setup exactly how its shown on its Github page

has_attached_file :avatar, default_url: "/assets/:style/missing.png"

I also created a paperclip.rb file inside my initializer directory with the code below:

Paperclip::Attachment.default_options[:default_url] = "/images/missing.png"

I proceeded to place my png file missing.png in my assets/images folder but no luck. I then decided to create an images folder within my public directory, public/images/missing.png but still got a broken image. With each change I've reset my rails server with no avail. Unlike the posts on here similar to the issue I'm not receiving any errors in particular, just an image not being found . What am I doing wrong?

Carl Edwards
  • 13,826
  • 11
  • 57
  • 119

3 Answers3

0

The line

has_attached_file :avatar, default_url: "/assets/:style/missing.png"

Implies that for all styles, you should have an image :

/assets/thumb/missing.png
/assets/big/missing.png

The default_options is useless in this case because you override it in the has_attached_file line.

Just set an image per style, or change your default_url to /assets/images/missing.png.

pierallard
  • 3,326
  • 3
  • 21
  • 48
  • Thanks I'll try this now. Also, should images like these be put in the public directory or the asset pipeline? Which directory is most common? – Carl Edwards Oct 21 '14 at 17:14
0

The solution involved two steps.

  1. Since I'm using default_url in my user model there was no need to have nor include paperclip.rb for this particular scenario
  2. Instead of using the url path specified by the wiki I put the image in my public/images directory and used: default_url: "missing.png" and everything worked.

Special thanks to Jared L Cowan's for the advice in the comment secion of his post

Carl Edwards
  • 13,826
  • 11
  • 57
  • 119
0

To add on to ForgetTheNorm's answer.

I think it's best to put the image in the public directory. Otherwise if you put it in assets/images you'll need to change the config.serve_static_assets to = true.

You can change that in config/environments/production around line 23

Also, you can add this 3 lines of jquery as a solution, or as an added measure to prevent broken images.

$("img").error(function() {
  $(this).unbind("error").attr("src", "PUT_YOUR_FOLDER_OR_URL_PATH_HERE");
});
Jared L Cowan
  • 56
  • 2
  • 7
  • Thanks for answering my question in ForgetTheNorm's post. Is there a rule of thumb on where to put certain images when it comes to the `public` and `assets` directories? – Carl Edwards Oct 21 '14 at 19:14
  • 1
    I would double check as I have only been doing rails for like 7months or something, but if it's an image that is used outside of your normal CSS (like for errors) put it in public. Don't put images that you use for your background etc. Here are some links that might help. - https://www.reinteractive.net/posts/116-12-tips-for-the-rails-asset-pipeline - http://stackoverflow.com/questions/8804225/when-to-put-images-to-app-asssets-and-when-to-public-images-in-rails-3-1 - http://stackoverflow.com/questions/7597812/image-showing-as-blank-in-rails-3-1-on-production-heroku?rq=1 – Jared L Cowan Oct 21 '14 at 19:29
  • Welcome. Hope it helps. – Jared L Cowan Oct 21 '14 at 19:55