# Nested Resources

**URL:** <https://www.ruby-forum.com/t/nested-resources/142860>\
**Category:** Rails\
**Created:** [July 12, 2008, 8:14am UTC](https://www.ruby-forum.com/t/nested-resources/142860 "2008-07-12T08:14:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![advait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19adc/32.png) [@advait](https://www.ruby-forum.com/u/advait)\
**Post date:** [July 12, 2008, 8:14am UTC](https://www.ruby-forum.com/t/nested-resources/142860/1 "2008-07-12T08:14:34Z")

</div>

How to make photo as a nested resource to album, where

class Album \< ActiveRecord::Base  
has\_many :photos  
end

class Photo \< ActiveRecord::Base  
belongs\_to : album  
end

Such that when I perform a show action an albums, I should be able to  
view only the photos associated with it and not all the photos of the  
table

---

<div class="post-metadata">

**Author:** ![advait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19adc/32.png) [@advait](https://www.ruby-forum.com/u/advait)\
**Post date:** [July 12, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/nested-resources/142860/2 "2008-07-12T12:30:54Z")

</div>

When you perform a show action on albums, the url will be something  
similar to “/albums/”

Then in the show method,

def show  
@album = Album.find(id)  
@photos = @album.photos

render template to display @photos  
end

this will display photos of a particular album alone.

On Jul 12, 11:14 am, Advait B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![advait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19adc/32.png) [@advait](https://www.ruby-forum.com/u/advait)\
**Post date:** [July 12, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/nested-resources/142860/3 "2008-07-12T19:50:57Z")

</div>

Unnikrishnan Kp wrote:

> When you perform a show action on albums, the url will be something  
> similar to “/albums/”
> 
> Then in the show method,
> 
> def show  
> @album = Album.find(id)  
> @photos = @album.photos
> 
> render template to display @photos  
> end
> 
> this will display photos of a particular album alone.
> 
> On Jul 12, 11:14 am, Advait B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I tried the above code and \<%=h @employees.find(:all) %\> in my  
show.rhtml file and then when I perform the show action it is displaying  
" # ".

Can you let me know where am I going wrong

---

<div class="post-metadata">

**Author:** ![advait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19adc/32.png) [@advait](https://www.ruby-forum.com/u/advait)\
**Post date:** [July 12, 2008, 7:56pm UTC](https://www.ruby-forum.com/t/nested-resources/142860/4 "2008-07-12T19:56:26Z")

</div>

> I tried the above code and \<%=h @employees.find(:all) %\> in my  
> show.rhtml file and then when I perform the show action it is displaying  
> " # ".
> 
> Can you let me know where am I going wrong

Sorry its \<%=h @photos.find(:all) %\> , I have written it as employee

---

<div class="post-metadata">

**Author:** ![advait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19adc/32.png) [@advait](https://www.ruby-forum.com/u/advait)\
**Post date:** [July 13, 2008, 2:45am UTC](https://www.ruby-forum.com/t/nested-resources/142860/5 "2008-07-13T02:45:35Z")

</div>

Hi Advait,

It looks like you’re displaying an array. You might want to do  
something more like this:

 \<% @photos.each do |current\_photo| %\> - Photo name: \<%= current\_photo.name %\>
 \<% end %\>

On Jul 12, 1:56 pm, Advait B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![advait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19adc/32.png) [@advait](https://www.ruby-forum.com/u/advait)\
**Post date:** [July 13, 2008, 7:00am UTC](https://www.ruby-forum.com/t/nested-resources/142860/6 "2008-07-13T07:00:00Z")

</div>

gsterndale wrote:

> Hi Advait,
> 
> It looks like you’re displaying an array. You might want to do  
> something more like this:
> 
> \<% @photos.each do |current\_photo| %\> - Photo name: \<%= current\_photo.name %\>
> \<% end %\>
> 
> On Jul 12, 1:56 pm, Advait B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thank You very much…Now every thing working wonderfully
