Question

Given that rake assets:clean completely empties the public/assets directory, I'm having somewhat of an issue figuring out how to store model instance assets.

So for example, I have a Wine model which uses Carrierwave to store several versions of an uploaded photo in public/assets/wines/[version]_[wine_name].[ext]. This works just fine. The asset is easily accessible.

But since it's stored in public/assets/*, running rake assets:clean will remove them! What should I do about this?

Was it helpful?

Solution 2

The answer is so mind-numbingly simple I can't believe I didn't think to try it earlier.

Carrierwave Example:

# It was this before
def store_dir
    "assets/photos#{ '/testing' if Rails.env.test? }"
end

# Now it's this
def store_dir
    "for/photos#{ '/testing' if Rails.env.test? }"
end

i.e., folder layout is as such:

..app/
    ../public
        ../assets # Contains only precompiled assets
        ../for # Contains only model instance assets

Regular precompiled assets resolve to http://mydomain.com/assets/* and instance assets resolve to http://mydomain.com/for/*. Specifically, I have a subdomain pointing to my assets called http://assets.mydomain.com, so for example it resolves to http://assets.mydomain.com/for/wines/some-asset.jpg.

This solves the issue of rake assets:clean because the /for directory is never touched. No need to explicitly host assets offsite.

Now as to whether or not I need to specify something else in production so the assets are actually served, I don't know - but at least in development this is working.

OTHER TIPS

I just did this for my portfolio site. Assets for users that will be uploaded via some sort of admin panel should be stored on the cloud. Something like an Amazon S3 bucket. Signup for amazon web services.

https://aws.amazon.com/

It's free up to a certain point. You should especially do this if you are using anything like heroku to deploy otherwise you could end up removing them on your next commit. I used paperclip and the setup was easy. Carrierwave is probably similar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top