# Accessing one controller from another

**URL:** <https://www.ruby-forum.com/t/accessing-one-controller-from-another/187863>\
**Category:** Rails\
**Created:** [April 30, 2010, 4:02pm UTC](https://www.ruby-forum.com/t/accessing-one-controller-from-another/187863 "2010-04-30T16:02:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 30, 2010, 4:02pm UTC](https://www.ruby-forum.com/t/accessing-one-controller-from-another/187863/1 "2010-04-30T16:02:05Z")

</div>

I have a posts controller and a pages controller for ‘static’ pages ie  
my homepage  
I want to have the latest post shown on the homepage… accessing the  
posts controller from within the pages controller

Is there a way I can do this?

thanks

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 30, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/accessing-one-controller-from-another/187863/2 "2010-04-30T16:40:26Z")

</div>

On Fri, Apr 30, 2010 at 7:02 AM, the batman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have a posts controller and a pages controller for ‘static’ pages ie  
> my homepage  
> I want to have the latest post shown on the homepage… accessing the  
> posts controller from within the pages controller

The posts controller doesn’t come into the picture; just get the post  
info in your pages controller.

def homepage  
@post = Post.latest # assuming a named scope  
render …

## HTH,

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 30, 2010, 4:58pm UTC](https://www.ruby-forum.com/t/accessing-one-controller-from-another/187863/3 "2010-04-30T16:58:02Z")

</div>

what is render for?

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 30, 2010, 5:13pm UTC](https://www.ruby-forum.com/t/accessing-one-controller-from-another/187863/4 "2010-04-30T17:13:16Z")

</div>

On Fri, Apr 30, 2010 at 7:58 AM, the batman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> what is render for?

I just put that there to represent standard controller stuff – whatever  
else you have in that controller method.

If you mean `what does render do?`, it’s time to open the API docs 🙂

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 30, 2010, 5:17pm UTC](https://www.ruby-forum.com/t/accessing-one-controller-from-another/187863/5 "2010-04-30T17:17:51Z")

</div>

that’s what I assumed but wasn’t sure if you meant render a partial…

got it working now  
thanks!

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 30, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/accessing-one-controller-from-another/187863/6 "2010-04-30T17:19:09Z")

</div>

in …/layout/application.rhtml

there will be home page  
\<%= render :partial =\> ‘layouts/homepage’ %\>  
in home page call your all pages …which u want to show

\<%= render :partial =\> ‘layouts/posts’ %\>  
\<%= render :partial =\> ‘layouts/news’ %\>  
…etc

-Shyam
