# Show parents subpages if on a subpage

**URL:** <https://www.ruby-forum.com/t/show-parents-subpages-if-on-a-subpage/183704>\
**Category:** Rails\
**Created:** [February 18, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/show-parents-subpages-if-on-a-subpage/183704 "2010-02-18T21:12:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lasse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/96bed5/32.png) [@Lasse](https://www.ruby-forum.com/u/Lasse)\
**Post date:** [February 18, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/show-parents-subpages-if-on-a-subpage/183704/1 "2010-02-18T21:12:22Z")

</div>

I am trying to display subpages from the subpages parent on a subpage.  
The code between the lines doesn’t work for me. Does anyone has an  
idea what would be a better code?  
pages and subpages are in the same controller.

@page = Page.find\_by\_name(params[:name])

## if @page.respond\_to?(‘subpages’) @subpages = @page.subpages

## elsif @page.parent.respond\_to?(‘subpages’) @subpages = @page.parent.subpages end

end

_ **application.html.erb** _

for page in @page  
show link to each page…  
end

for page in @subpages  
show link to each subpage…  
end

---

<div class="post-metadata">

**Author:** ![Lasse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/96bed5/32.png) [@Lasse](https://www.ruby-forum.com/u/Lasse)\
**Post date:** [February 18, 2010, 9:24pm UTC](https://www.ruby-forum.com/t/show-parents-subpages-if-on-a-subpage/183704/2 "2010-02-18T21:24:40Z")

</div>

Should acts\_as\_tree help me out?

---

<div class="post-metadata">

**Author:** ![Lasse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/96bed5/32.png) [@Lasse](https://www.ruby-forum.com/u/Lasse)\
**Post date:** [February 19, 2010, 11:32am UTC](https://www.ruby-forum.com/t/show-parents-subpages-if-on-a-subpage/183704/3 "2010-02-19T11:32:53Z")

</div>

Lasse wrote:

> Should acts\_as\_tree help me out?

Hi,  
As you need hierarchical structure. and straight forward parent -\>  
child relationship so, you will use acts\_as\_tree.

---

<div class="post-metadata">

**Author:** ![Lasse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/96bed5/32.png) [@Lasse](https://www.ruby-forum.com/u/Lasse)\
**Post date:** [February 19, 2010, 3:10pm UTC](https://www.ruby-forum.com/t/show-parents-subpages-if-on-a-subpage/183704/4 "2010-02-19T15:10:03Z")

</div>

Lasse wrote:

> Should acts\_as\_tree help me out?

I’m not sure. But what doesn’t work about your original code?

Anyway, if you need a tree plugin, skip acts\_as\_tree and use  
awesome\_nested\_set. It’s far more efficient for reads.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
