# Extending controllers

**URL:** <https://www.ruby-forum.com/t/extending-controllers/115942>\
**Category:** Rails\
**Created:** [September 30, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/extending-controllers/115942 "2007-09-30T19:07:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a8b319/32.png) [@eben](https://www.ruby-forum.com/u/eben)\
**Post date:** [September 30, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/extending-controllers/115942/1 "2007-09-30T19:07:41Z")

</div>

I’m fairly new to RoR, so apologies in advance if I rape the  
terminology here…

I am trying to use functionality from one controller in another  
controller. Here is the first controller:

class PagesController \< ApplicationController

def show  
@page = Page.find(params[:id])  
end

end

2nd controller

class SiteController \< ApplicationController  
def some\_page  
@page = Page.find(:all, :conditions =\> “name = ‘some\_page’”)  
end  
end

Ultiamtely, I’m trying to access my “Pages” table from a controller  
other than the Pages controller… not sure if I have to extend the  
new controller or how the functionality “trickles down” to my new  
controller?

Any advice is appreciated

---

<div class="post-metadata">

**Author:** ![eben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a8b319/32.png) [@eben](https://www.ruby-forum.com/u/eben)\
**Post date:** [September 30, 2007, 7:16pm UTC](https://www.ruby-forum.com/t/extending-controllers/115942/2 "2007-09-30T19:16:29Z")

</div>

You don’t need to do anything in particular. The Page model is  
available in the same way from whatever controller or other model  
you’re using.

Fred

---

<div class="post-metadata">

**Author:** ![eben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a8b319/32.png) [@eben](https://www.ruby-forum.com/u/eben)\
**Post date:** [September 30, 2007, 7:47pm UTC](https://www.ruby-forum.com/t/extending-controllers/115942/3 "2007-09-30T19:47:44Z")

</div>

Thanks for the response, that’s what I was hoping was the case. One  
more question:

This code in the “show” Page view works fine

\<% for column in Page.content\_columns %\>

**\<%= column.human\_name %\>:** \<%=h @page.send(column.name) %\>

\<% end %\>

However if I copy that into the “about” view in the new controller, I  
get this error:

undefined method `name’ for #Array:0xb799d1f0

Note, I’m using this to fetch the data in the controller: @page =  
Page.find(:all, :conditions =\> “name = ‘some\_page’”)

\<%= debug(@page) %\> shows a valid record returned in the Page object,  
so I’m unclear why the for loop would be failing?

thanks,  
Eben

On Sep 30, 1:16 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a8b319/32.png) [@eben](https://www.ruby-forum.com/u/eben)\
**Post date:** [September 30, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/extending-controllers/115942/4 "2007-09-30T19:54:51Z")

</div>

makes sense, thanks for your time

Eben

---

<div class="post-metadata">

**Author:** ![eben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a8b319/32.png) [@eben](https://www.ruby-forum.com/u/eben)\
**Post date:** [September 30, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/extending-controllers/115942/5 "2007-09-30T19:51:56Z")

</div>

The problem is that in your show action, @page is a single record. In  
your “about” action, you are using :all in find, which returns an array.  
If you just want a single record, change it to :first. If you want to  
use the array, do it like this:

\<% @pages.each do |page| -%\>  
\<% for column in Page.content\_columns %\>

**\<%= column.human\_name %\>:** \<%=h page.send(column.name) %\>

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