Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A route to serve static assets (like .jpgs, etc?)

I've worked my way through a number of interesting routing problems - turning a request URL into a hash, etc., but just out of curiosity, is there a way to tell the routing system that you want anything that comes under a certain url subpath to be served literally - without going through a controller?

For instance, if I have /home/me/public_html/rails_proj/images/foo.jpg, and .../rails_proj/images/other/bar.jpg, can I insert a route that says "anything under images should just be served as an object of the default mime type?"

Might be interesting.

like image 667
Sniggerfardimungus Avatar asked Feb 10 '09 05:02

Sniggerfardimungus


4 Answers

If you put the "images" directory into the "public" folder of the Rails app (for example: /public/images/) then you shouldn't have any problems with MIME types unless your web server is configured wrongly.

According to your examples, you want the images dir in the root of the app. I don't think there is a way though Rails to make those images visible, but if you really wanted to you could use mod_rewrite to make it work. Once again, it would be up to the web server to make sure the images had the correct MIME type.

like image 163
Nick Messick Avatar answered Nov 16 '22 02:11

Nick Messick


Things that are served out of the public directory will not go through Rails - they'll just be handled by your server (probably apache). The only reason why you would need to serve images through the rails system is if you wanted some kind of control on who could access them. Just put everything else in public and access ala: siteurl.whatever/images/*.jpg

like image 32
Allyn Avatar answered Nov 16 '22 01:11

Allyn


I typically use nginx as a frontend and Apache/Passenger as a backend. Ngingx proxies all Rails requests to Apache but handles all static content itself. Check out the examples on the English nginx wiki. Here is a small excerpt for nginx config:

server {
    listen 80;
    server_name www.domain.com;
    location ~* \.(jpg|jpeg|gif|png|ico|css|bmp|js)$ {
        root   /path/to/static/assets/dir;
    }
    location / {
        proxy_pass http://127.0.0.1:81;
    }
}

So have apache listen on port 81 to handle Rails requests proxied by nginx and let nginx deliver static content. Not only is nginx supposedly faster than Apache at delivering static content, but this also offloads your Rails application for every image, stylesheet, javascript or whatever other static content.

like image 5
ronaldevers Avatar answered Nov 16 '22 03:11

ronaldevers


I think the simplest way solve this problem is just using image_path helper method, which provide you the path for the image you want to display in the view. For example, if you want to refer a logo.png under the /assets/images/logo.png, you can just use image_path('logo.png').

like image 3
Yang Dong Avatar answered Nov 16 '22 02:11

Yang Dong