# Accessing uploaded file on filesystem question

**URL:** <https://www.ruby-forum.com/t/accessing-uploaded-file-on-filesystem-question/60186>\
**Category:** Rails\
**Created:** [May 10, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/accessing-uploaded-file-on-filesystem-question/60186 "2006-05-10T23:44:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 10, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/accessing-uploaded-file-on-filesystem-question/60186/1 "2006-05-10T23:44:28Z")

</div>

Hi,

I’m able to upload files onto my webserver filesystem but I had a  
question  
regarding how I’d go about accessing them using the ![]()  
tag.  
If my files, i.e pictures, are being uploaded to  
public/images//filename.jpg, then should I store the entire path,  
or  
just the picture name in my database table? I would imagine storing  
just  
the filename is better so that it’d be easy to move the file location  
later. Also, if I’m using \<img src= \<%= @path %\>\>, then what should the  
path be? What is the root directory from where it starts to try to find  
the  
file? Is it the view directory under app, OR, does it start from the  
directory that holds the public and app directories? Should the path be  
something like - ![]() ? I tried  
this  
but it couldn’t find the file?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 10, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/accessing-uploaded-file-on-filesystem-question/60186/2 "2006-05-10T23:50:45Z")

</div>

Hi Sam,

Static files will be found under the /public directory.

![]()

maps to

/your\_rails\_app/public/images/your\_image.jpg

–  
Scott B.  
Electro Interactive, Inc.  
Web: [http://www.ElectroInteractive.com](http://www.ElectroInteractive.com)  
Blog: [http://synthesis.sbecker.net](http://synthesis.sbecker.net)

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 11, 2006, 12:24am UTC](https://www.ruby-forum.com/t/accessing-uploaded-file-on-filesystem-question/60186/3 "2006-05-11T00:24:37Z")

</div>

Hi,  
You should just be able to use the \<%= image\_tag %\> tag as well.  
So if you upload the pictures in a folder associated with a user (ie.  
/your\_rails\_app/public/images/username/image.jpg)

\<%= image\_tag “#{current\_user.login}/#{image.filename}”, :alt  
=\>“#{image.description}” %\>

Of course this example is presuming that you have implemented a  
current\_user method for use in your views and that you have a filename  
and a description field for your images. You should always include an  
alt tag in case someone is using a screen reader or surfing with images  
turn off. The alt will also appear as a tool tip on most browsers, which  
is a nice little bonus as well.

If you want to make this approach a bit more flexible you could put any  
logic into a helper.

#helpers/somehelper.rb  
def user\_image filename  
if current\_user  
“#{current\_user.login}/#{filename}”  
else  
“guest.jpg”  
end  
end

\<%= image\_tag user\_image(image.filename), :alt =\>“#{image.description}”  
%\>

Now you can call method all over your app and if you change where you  
put your uploaded images, you only have to make changes in one place.

-Eric G.  
[ericgoodwin.com](http://ericgoodwin.com)

Scott B. wrote:

> –
> 
> > regarding how I’d go about accessing them using the ![]()  
> > to find the file? Is it the view directory under app, OR, does it start  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Eric G.  
[http://www.ericgoodwin.com](http://www.ericgoodwin.com)
