0
votes

I am using paperclip to upload user avatar. Here is User model:

has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "user.png", :path => "app/assets/images/:class/:attachment/:id/:basename_:style.:extension", :url => ":class/:attachment/:id/:basename_:style.:extension"

So image is saving in app/assets/images/user/avatar/:id/:basename_:style.:extension

But when I do

<%= image_tag @user.avatar.url %>

It shown as:

<img src="/images/users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original.jpg" alt="99e88dc27c19d8c6163d9cd305f738be original">

i.e. inserts "/images" instead of "/assets"

I double-checked, the avatar image exists in the assets/images/user/avatar/ folder

Although all other images in page are showing correctly using assets pipeline "/assets/logo-thebighashgohere.png"

NOTE: This works correctly if I manually insert image url as string i.e.:

<%= image_tag "users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original.jpg" %>

It correctly shows as

<img src="/assets/users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original-thebighashgohere.jpg" alt="99e88dc27c19d8c6163d9cd305f738be original thebighashgohere">
1

1 Answers

1
votes

I strongly recommend you to not save user-generated-content into the assets folder!

if your website is going into production mode, the assets are compiled and everything you throw in at runtime won't be catched. stuff like that belongs into the /public directory (!)

to solve your problem

 :url => ":class/:attachment/:id/:basename_:style.:extension

you tell paperclip how to generate your "url". with "path" you define where the files are stored internally, with url you control how to generate the routes. your rote is wrong, no asstes path in it.

but again - do not save those picutres into assets !


btw: i wonder if your solution is even possible in productionmode. the assetspipeline is generting files with a digest appending on it, while paperclip knows nothing about those digest it will always render a route without the digest-stamp. by that you can't call the images from the assets pipeline. so your whole concept won't work in production, but i might be wrong