# Location of images

**URL:** <https://www.ruby-forum.com/t/location-of-images/130210>\
**Category:** Rails\
**Created:** [February 26, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/location-of-images/130210 "2008-02-26T12:30:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 26, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/location-of-images/130210/1 "2008-02-26T12:30:15Z")

</div>

hello all,

I’m new to RoR. I am reading Agile Development etc. and I have  
completed a tutorial that explains the differences between rails 2.0  
and rails 2.1.

Still some questions are unanswered by the book. I hope you can help  
me with the most intriguing one:  
I have created a scaffold for model ‘movie’. After some iterations I  
have manually added a admin controller with views.  
It appears that the image-links from the movie-view result in a get  
from the image in the directory public/images, whereas the image links  
from the admin views get the images another place.

Can you explain WHY this happens and what I can do to p be able to

- use public/images in both cases
- use the same subdirectory of a view directory in both cases?

thanks very much!  
Ruud

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 26, 2008, 12:43pm UTC](https://www.ruby-forum.com/t/location-of-images/130210/2 "2008-02-26T12:43:50Z")

</div>

Please can u paste the code in question and i will tell u, thanks.

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 26, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/location-of-images/130210/3 "2008-02-26T13:09:17Z")

</div>

Rails by default will look into the public/images for any image name u  
give.

If u have yr image in a separate folder like public/my\_images folder  
then in yr view file u need to mention the image path as  
“my\_images/image.jpg”

> Can you explain WHY this happens and what I can do to p be able to
> 
> - use public/images in both cases
> - use the same subdirectory of a view directory in both cases?
> 
> thanks very much!  
> Ruud

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 26, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/location-of-images/130210/4 "2008-02-26T15:38:55Z")

</div>

I believe the issue is with the way you’re rendering the image tag.  
This line:

 ![]()

Is a _relative path_. You should be able to use an absolute path “/  
images/…” which will be absolute with respect to your Rails  
application.

It’s probably also worth mentioning that, if you’re storing images in  
the standard /public/images folder, you can use a rails helper:

\<%= image\_tag move.one\_sheet\_url, :alt=\>"#{movie.name} pic" %\>

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 26, 2008, 4:17pm UTC](https://www.ruby-forum.com/t/location-of-images/130210/5 "2008-02-26T16:17:46Z")

</div>

On 26 feb, 15:38, AndyV [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I believe the issue is with the way you’re rendering the image tag.  
> This line:
> 
> ![]()
> 
> Is a _relative path_. You should be able to use an absolute path “/  
> images/…” which will be absolute with respect to your Rails  
> application.

Yes, that is true. It IS relative. I am going to implement a helper  
function as you suggested. That is much better.  
But I still would like to know why movie.one\_sheet\_url() gives me  
another result when called from two different locations  
(in this case views/movies/index.html.erb and views/ and  
views/login/list\_movies.html.erb)

Rails has so much magic going on that a newbie like me gets a bit  
dizzy.

> It’s probably also worth mentioning that, if you’re storing images in  
> the standard /public/images folder, you can use a rails helper:
> 
> \<%= image\_tag move.one\_sheet\_url, :alt=\>"#{movie.name} pic" %\>

Thanks, I wil certainly try this. Good opportunity to have a look at  
helper functions as well!

Ruud

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 26, 2008, 1:28pm UTC](https://www.ruby-forum.com/t/location-of-images/130210/6 "2008-02-26T13:28:43Z")

</div>

in fact, I don’t use the login\_controller which uses the admin layout.  
What’s in a name.

There is a difference between a list of the movies from the login view  
and the movies view.  
The admin layout includes the following lines:

```
    <% if session[:user_id] %>
        <% gebruiker = User.find_by_id( session[:user_id]).naam -

```

## %\>

# \<%= gebruiker -%\>
  
\<%= link\_to( ‘overzicht films’, :action =\> ‘list\_movies’ )  
-%\>  
  
\<% else %\>  

# no user
  
\<% end %\>

In the login controller I have

## def list\_movies @all\_movies = Movie.find( :all) @all\_movies = [] if !@all\_movies end

and in the views/login list\_movies.html.erb contains

# Overzicht van films

\<% if flash[:notice] %\>

\<%= flash[:notice] -%\>
  
\<% end %\>

\<% i = 0  
for movie in @all\_movies %\>  
\<%= i += 1  
(i % 2 == 0 ? ‘

’ : ‘’) -%\>

```
<td><img src="images/<%= movie.one_sheet_url %>"/></td>
<td class='titel'>
        <b><%= movie.title %></b><br>
        <%=h movie.description %>
</td>
<td>
    <table>
        <tr><td class='actie'><%= link_to( 'toon', {
                    :controller => 'movies',
                    :action => 'show',
                    :id => movie}) -%></td></

```

tr\>

\</  
tr\>

| | beschrijving | acties |
| --- | --- | --- |
|
| |
|
| |
| \<%= link\_to  
‘bewerk’, edit\_movie\_path(movie) %\> |
| \<%= link\_to 'verwijder', movie, :confirm =\> 'Zeker weten?', :method =\> :delete %\> |

 \<% end %\> -----------------------------------

Following the link ‘list users’ from the admin layout, I get a table  
in the browser, without the images displayed. Webrick says

## [http://localhost:3000/login/list\_movies](http://localhost:3000/login/list_movies) → /login/images/shrekI.png 127.0.0.1 - - [26/Feb/2008:13:14:08 CET] “GET /login/images/ shrekII.png HTTP/1.1” 404 571 [http://localhost:3000/login/list\_movies](http://localhost:3000/login/list_movies) → /login/images/shrekII.png 127.0.0.1 - - [26/Feb/2008:13:14:09 CET] “GET /login/images/ shrekIII.png HTTP/1.1” 404 571 [http://localhost:3000/login/list\_movies](http://localhost:3000/login/list_movies) → /login/images/shrekIII.png 127.0.0.1 - - [26/Feb/2008:13:14:09 CET] “GET /login/images/BtoF.png HTTP/1.1” 404 571

The images are in public/images, so that explains why they are not  
displayed  
Now, if I go to localhost:3000/movies, I get a listing of the movies  
too;  
movies/index.html.erb contains

# Listing movies

\<% if flash[:notice] %\>

\<%= flash[:notice] -%\>
  
\<% end %\>

\<% i = 0  
for movie in @movies %\>  
\<%= i += 1  
(i % 2 == 0 ? ‘

’ : ‘’) -%\>

```
<td><img src="images/<%= movie.one_sheet_url %>"/></td>
<td class='titel'>
        <b><%= movie.title %></b><br>
        <%=h movie.description %>
</td>
<td>
    <table>
        <tr><td class='actie'><%= link_to 'bewerk',

```

edit\_movie\_path(movie) %\>

  
\</  
tr\>  
\<%= button\_to( “doe in winkelmand”, { :action  
=\> :add\_to\_cart,  
:id =\> movie })  
%\>  

| | beschrijving | acties |
| --- | --- | --- |
|
| |
|
| |
| \<%= link\_to ‘verwijder’,  
movie, :confirm =\> ‘Zeker weten?’,  
:method =\> :delete %\> |
|
| |

  

 \<% end %\>  

\<%= button\_to( "leeg winkelmand", { :action =\> :empty\_cart } ) -%\> \<%= link\_to 'Nieuwe film', new\_movie\_path() %\> --------------------------------

As you can see, it is almost the same as list\_movies.html.erb.  
But now, the images ARE displayed and webrick says

127.0.0.1 - - [26/Feb/2008:13:14:08 CET] “GET /login/images/shrekI.png  
HTTP/1.1” 404 571  
[http://localhost:3000/login/list\_movies](http://localhost:3000/login/list_movies) → /login/images/shrekI.png  
127.0.0.1 - - [26/Feb/2008:13:14:08 CET] “GET /login/images/  
shrekII.png HTTP/1.1” 404 571  
[http://localhost:3000/login/list\_movies](http://localhost:3000/login/list_movies) → /login/images/shrekII.png  
127.0.0.1 - - [26/Feb/2008:13:14:09 CET] “GET /login/images/  
shrekIII.png HTTP/1.1” 404 571  
[http://localhost:3000/login/list\_movies](http://localhost:3000/login/list_movies) → /login/images/shrekIII.png

I am puzzled. If you can shine a light on this I’d be very grateful.  
Let me now if I have to provide extra information…

Ruud

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 27, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/location-of-images/130210/7 "2008-02-27T14:33:09Z")

</div>

On 2/26/08, ruud [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > images/…" which will be absolute with respect to your Rails  
> > application.
> 
> Yes, that is true. It IS relative. I am going to implement a helper  
> function as you suggested. That is much better.  
> But I still would like to know why movie.one\_sheet\_url() gives me  
> another result when called from two different locations  
> (in this case views/movies/index.html.erb and views/ and  
> views/login/list\_movies.html.erb)

Because it’s RELATIVE so if in one case it’s relative to views/movies,  
and in the other to views/login

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 27, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/location-of-images/130210/8 "2008-02-27T15:05:07Z")

</div>

+1… sorry I should have been more specific.
