5

I have my Rails 3.1.0 application running with passenger in production environment and I have a section where the application allows the user to change his profile picture so I upload the image using an ajax uploader and in my controller I upload the file and generate different sizes for the image with rmagick then I render the new image with an image_tag but the application won't show the image till I restart the server.

What I get is No route matches [GET] "assets/path/to/image.png"

If I restart the server It will show the image, but obviously I can't be restarting the server every once a user uploads a new image.

How can I solve the keeping the assets working the right way?

Mr_Nizzle
  • 6,644
  • 12
  • 55
  • 85

1 Answers1

6

The Rails asset pipeline is really meant for structural / design images, such as backgrounds, icons, banners, etc..). Dynamic assets should go in the public directory [source below]

It's probably a good idea to serve static assets through Nginx or Apache or whatever your web-server is, or place them in the public directory of your Rails app.

That should solve your problem right there.. e.g. make a separate path for static assets into which you upload those images with rmagick / carrierwave, or whatever gem you prefer.

The asset pipeline only knows about images which are present during start-up. So separating static / uploaded assets into a separate directory , and serving it directly through the web-server, will help -- it should also be much faster.

you'll need something like this in your configuration:

# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false

# Compress JavaScripts and CSS
config.assets.compress = true

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

# Generate digests for assets URLs
config.assets.digest = true

# UNCOMMENT the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

More General:

http://railscasts.com/episodes/279-understanding-the-asset-pipeline

http://guides.rubyonrails.org/asset_pipeline.html

Rails 3.1: Should File Uploads be added to asset pipeline?

Regarding serving images outside asset pipeline:

http://mrjaba.posterous.com/rails-31-asset-pipeline-with-nginx-and-passen

http://trackingrails.com/posts/rails-31-and-asset-pipeline-problems-with-apache

http://pastebin.com/kC4Ba40U

https://github.com/defunkt/resque/issues/418

Community
  • 1
  • 1
Tilo
  • 33,354
  • 5
  • 79
  • 106
  • The situation is that I'm deploying my `Rails 3.1.0` application on the root of my web server so I don't have another way to serve file but to do this trough the application it self later on we'll be using a CDN or Amazon's S3 but right now we'll be serving the images trough the rails applications, I saw i way for disabling assets that comes as default on rails 3 but we're now using other advantages of working with assets and using rails core as it comes, thanks. – Mr_Nizzle Oct 17 '11 at 15:00
  • you could still do this outside of Rails, e.g. tell your Apache or Nginx web server to serve an additional directory, which doesn't have a route in Rails. – Tilo Oct 17 '11 at 21:51
  • do you have some info about that with apache? because I'm now serving the application on the root `/` of my domain, Thanks. – Mr_Nizzle Nov 10 '11 at 20:29
  • and does it have something to do with this http://guides.rubyonrails.org/asset_pipeline.html#x-sendfile-headers ? – Mr_Nizzle Nov 11 '11 at 19:50
  • 1
    yes, you should also read-up on the new asset pipeline -- that has changed a lot in 3.1 : http://railscasts.com/episodes/279-understanding-the-asset-pipeline and perhaps http://jackchu.com/rails-31-asset-pipeline-content-delivery-netw – Tilo Nov 11 '11 at 22:50
  • @Mr_Nizzle : x-sendfile-headers ... yes! – Tilo Nov 18 '11 at 17:47
  • Didn't success on XSendFile and all this thins, so far because It's the first stage on the project I decided to make a CNAME so now I have `image.mydomain.com` and this points directly to `/path/to/rails_app/app/assets/` now It's is kind of ready to use later with a CDN. Thanks @Tilo . – Mr_Nizzle Nov 19 '11 at 05:22