views:

11

answers:

1

Hi,

In my Rails application, user may have custom page templates (not for the application) which are stored in public/user_templates/user1/

the view .erb file is also stored there in the user template folder. There is an images folder inside every template folder. How can I use the relative paths of the images in the .erb file?

For example <img src"images/image1" />

Thanks, Imran

+1  A: 

You can't access static files inside of your app directory without passing them through a route somehow. You should create a directory for each user_id in the public/images folder of your application, then store the images for each user there. Then you can access those images at <img src="images/#{user_id}/image1.jpg" /> or with a view helper image_tag "#{user_id}/image1.jpg" Depending on your application, you may want to use Paperclip

Daniel Mendel
Thanks Daniel. We are already using the same thing -- with a slight variation though i.e. referencing the same user folder through user_id where the .erb file and images folder is stored.
Imran
Then I suppose you'll have to create an image-serving controller and corresponding route to serve those files, OR a piece of Rack middleware to catch those image requests and serve the static files, the latter is probably a performance boost over the former.
Daniel Mendel
hmm... Could you please show me an example, as I haven't worked much with Rack?
Imran
I can recommend starting with Ryan Bate's ever-helpful screencast: http://railscasts.com/episodes/151-rack-middleware
Daniel Mendel
OK, thanks for your help Daniel.
Imran