Serving files from public


I am building a rails app which supports accounts based on subdomain
names. I want to serve files in public with respect to the subdomain
name. For example:

<img src="images/bar.jpg"> should deliver public/foo/images/bar.jpg.

Is there any way to do this in rails? Or should I use the
Mongrel::HttpHandler and response.send_data?



Using the account_location plugin, something like this should work:


Although I would advise you to create a separate folder in public to store all your user images, otherwise your public folder will become a huge collection of folders pertaining to rails and your customers. Hard to keep track of them and back them up efficiently and you’ll have serious problems if you deploy via capistrano if you use your method.

Something like /public/user_images/subdomain/image.jpg would be more appropriate.

Best regards

Peter De Berdt

Thanks for the reply Peter. I should have been clearer in my post: the
idea is that users will be able to add content via Textile, meaning that
Rails helpers will not be used for generating image or link paths.

Then how are you going to solve your problem of displaying images?

I’ve done something quite similar to what you have done, and this is my setup:

  • Allow user to upload a collection of images (uses attachment_fu), I’ve added the ability to shove them into albums and add some searchable tags to it

  • Below my textarea (using Textile) they can browse through their images, select one and then click the “Insert” button. This could insert the correct the !image_url! text into the textarea. I’ve done it a bit differently and have added my own tag parsing method (I don’t remember it too well, it’s an old project), something like &my_image.jpg& so that the user doesn’t even see this is translated to /user_images/subdomain/my_image.jpg when saving the record.

And my comment on having to be able to easily symlink the folder containing the user images when deploying via capistrano still stands.

Best regards

Peter De Berdt

if you need moreour control, set up mod_xsendfile and keep images
outside of public
there should be plugin, to make it even easier