0
votes

My rails' app has uploading images function.

Precompiled assets will be created in public/assets When I deploy rails app in production and run

RAILS_ENV=production rake assets:precompile

I will get below error messages every time user upload images successfully in admin platform and request showing that created image. ActionView::Template::Error (The asset "course/main_photo/XXXX/Course_ss12344.png" is not present in the asset pipeline.):

In those messages, I found that images created by user didn't precompile in the public/assets.

config.assets.compile = true # when I set true for config.assets.compile, server will response assets slowly that is not good choice.

config/environments/production.rb  # below is my relative code in production's config
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.serve_static_assets = true

Can anyone give me some advice on how to auto precompile assets or other advice when user upload images online?

1
Is course/main_photo/XXXX/Course_ss12344.png an image you expect to exist? Or being uploaded by a user? How is the image link from the view?spickermann
course/main_photo/XXXX/Course_ss12344.png <-- that is user created. I created course admin page for user to upload their course and relative images. The path of image uploading will created in app/assets/images/course/main_photo/model_id/xxx.png. Yes, image link is from view.Tiger oil
That means you have a hardcoded link in a view to an image that must be uploaded from a user and may not exists? And you store those user uploads in the assets folder on the server?spickermann
@spickermann Thanks for replying. I use html image tag(<img>) and set src to image_path(course/main_photo/xxxx/xxx.png). I can sure that these images uploaded by user exist in app/assets/images/course/xxxx/. I am wondering that assets.compile setting to false alwayse search for public/assets/course/main_photo/xxxx/encoded_name.png. Means that the new created images by user in production must run again rake assets command to rebuild that images in public/assets/..... How the people do automatically(run by rails or doing some tricky way ) precompile assets.Tiger oil
I wonder why you took this uncommon approach? Usually – IMHO all – articles about this topic suggest that files uploaded by a user are stored external or in the public/system folder. And more importable that user uploads do not get delivered via the asset pipeline. This approach fights against common Rails conventions (see the Rails Guide about user uploads) and is error-prone (What if that file does not exist? What if you want to use an environment like Heroku or multiple servers?). This sounds like a xy problem to me.spickermann

1 Answers

0
votes

Try this Remove previous pre-compiled assets by running "rake assets:clobber" than do "rake assets:precompile" and Restart your server