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.
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
Blog: http://synthesis.sbecker.net
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 ![]()
to find the file? Is it the view directory under app, OR, does it start
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
–
Eric G.
http://www.ericgoodwin.com