Sam Donaldson
2006-May-10 21:41 UTC
[Rails] accessing uploaded file on filesystem question.
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. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060510/7c86c3b4/attachment.html
Scott Becker
2006-May-10 21:50 UTC
[Rails] accessing uploaded file on filesystem question.
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 Becker Electro Interactive, Inc. Web: http://www.ElectroInteractive.com Blog: http://synthesis.sbecker.net On 5/10/06, Sam Donaldson <samonderous@gmail.com> wrote:> 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. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Eric Goodwin
2006-May-10 22:22 UTC
[Rails] accessing uploaded file on filesystem question.
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 Goodwin ericgoodwin.com Scott Becker wrote:> 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 Becker > Electro Interactive, Inc. > Web: http://www.ElectroInteractive.com > Blog: http://synthesis.sbecker.net > > On 5/10/06, Sam Donaldson <samonderous@gmail.com> wrote: >> 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. >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- Eric Goodwin http://www.ericgoodwin.com