When coding a views/.rhtml file, where do the various directories fall
in relation to my app. For example, if I have a file in public/images of
“image1.jpg”, I need to code a “” for it (I think, I
won’t be shocked if someone says "in rails we do it this way). IF I’m
coding the tag, where is the file … what directory path do I put
in so I can find image1.jpg?
You can make absolute references to your images using
/images/image_name.gif from your views. This also applies to
JavaScripts and stylesheets, which map to /javascripts and /stylesheets
respectively. Of course you can use relative references, but I’ve found
the safest bet to be absolute.
When coding a views/.rhtml file, where do the various directories fall
in relation to my app. For example, if I have a file in public/images of
“image1.jpg”, I need to code a “” for it (I think, I
won’t be shocked if someone says "in rails we do it this way). IF I’m
coding the tag, where is the file … what directory path do I put
in so I can find image1.jpg?
I would just do…
<%= image_tag “image1.jpg” %>
which will result in html similar to:
Which will look in RAILS_ROOT/public/images/image1.jpg for that file.
When coding a views/.rhtml file, where do the various directories fall
in relation to my app. For example, if I have a file in public/images of
“image1.jpg”, I need to code a “” for it (I think, I
won’t be shocked if someone says "in rails we do it this way). IF I’m
coding the tag, where is the file … what directory path do I put
in so I can find image1.jpg?
As far as your webserver is concerned, the root of your Rails app is
the ‘public’ directory. So if you have an image called image1.jpg in
public/images, you can get to it using
Incidentally, Rails has a built-in image tag helper that assumes that
images are stored in the /images directory, so in this case you could
use
Incidentally, Rails has a built-in image tag helper that assumes that
images are stored in the /images directory, so in this case you could
use
<%= image_tag(“image1.jpg”) %>
Thanks, Chris, for both pieces of information. I was really looking for
the rails helper (I’d guessed it existed but couldn’t find the
reference), but also wanted to know where things were in case I needed
them.
Thanks again.
—Michael
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.