Forum: Ruby on Rails accessing uploaded file on filesystem question.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sam D. (Guest)
on 2006-05-11 01:44
(Received via mailing list)
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 <img src="..." >
tag.
If my files, i.e pictures, are being uploaded to
public/images/<dir>/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 - <img src="public/images/<dir/filename.jpg"> ?  I tried
this
but it couldn't find the file?

Thanks in advance.
Scott B. (Guest)
on 2006-05-11 01:50
(Received via mailing list)
Hi Sam,

Static files will be found under the /public directory.

<img src="images/your_image.jpg">

maps to

/your_rails_app/public/images/your_image.jpg

--
Scott B.
Electro Interactive, Inc.
Web: http://www.ElectroInteractive.com
Blog: http://synthesis.sbecker.net
Eric G. (Guest)
on 2006-05-11 02:24
(Received via mailing list)
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

Scott B. wrote:
> --
>> regarding how I'd go about accessing them using the <img src="..." >
>> 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
>>
>>
>>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails


--
Eric G.
http://www.ericgoodwin.com
This topic is locked and can not be replied to.