URL from file path

What’s the best way to get the URL for a resource when my ruby code has
the full pathname. I want to turn
/var/rails/project/public/images/image.png into /images/image.png

Jeremy W.
Serval Systems Ltd.

www.servalsystems.co.uk http://www.servalsystems.co.uk
Tel: 01342 331940
Fax: 01342 331950

you could use File.basename to return just the filename then prepend
with ‘/images/’

On 8/25/06, Jeremy W. [email protected] wrote:

What’s the best way to get the URL for a resource when my ruby code has the
full pathname. I want to turn
/var/rails/project/public/images/image.png into
/images/image.png

Have you tried something like:

resource_path.slice(“#{ File.expand_path(RAILS_ROOT) }/public”.length,
resource_path.length)

There’s probably a much better way!

Matt

thanks, that’s the one. previously I was using
Pathname.new(“.”).realpath to get the rails root, but that seemed to
have problems under windows.

Matthew D. wrote:

resource_path.slice(“#{ File.expand_path(RAILS_ROOT) }/public”.length,
resource_path.length)

There’s probably a much better way!

Matt

Jeremy W.
Serval Systems Ltd.

www.servalsystems.co.uk http://www.servalsystems.co.uk
Tel: 01342 331940
Fax: 01342 331950