# Accessing link\_to from within a controller

**URL:** <https://www.ruby-forum.com/t/accessing-link-to-from-within-a-controller/47905>\
**Category:** Rails\
**Created:** [November 15, 2005, 1:14am UTC](https://www.ruby-forum.com/t/accessing-link-to-from-within-a-controller/47905 "2005-11-15T01:14:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![icottee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3be4f8/32.png) [@icottee](https://www.ruby-forum.com/u/icottee)\
**Post date:** [November 15, 2005, 1:14am UTC](https://www.ruby-forum.com/t/accessing-link-to-from-within-a-controller/47905/1 "2005-11-15T01:14:50Z")

</div>

Hi all

I want to build up a basic menu system for my application. Each  
‘section’ has it’s own left hand menu and I was planning on handling  
that by defining a ‘menu’ method within each controller, which I’d  
pass (along with any other menus I needed at that point) in an  
instance variables called @menus. So I’d get something like

def menu  
{:title =\> ‘Vehicles’,  
:items =\> [  
link\_to(‘List all’, :action =\> ‘index’),  
link\_to(‘Search’, :action =\> ‘search’),  
link\_to(‘Do something else’, :action =\> ‘bang’),  
]  
}  
end

def list  
@menus = [self.menu]  
end

My application layout looks for @menus and displays the menus in the  
left hand side. Problem is I can’t access link\_to from my controller  
as it’s a helper. The FAQ mentions writing your own helpers within  
the controller but in this case I want to access a default helper  
from the controller.

Question 1: Why is there this restriction?  
Question 2: How do I get around it? 🙂

Many thanks for any suggestions. I’ve got ‘The Book’ so if there are  
clues in there I’ve missed I’m happy for page numbers. Or if I am  
just doing it the wrong way - educate me as to the right way please.

Many thanks once again

Ian Cottee  
Blue Fountain Systems Ltd

---

<div class="post-metadata">

**Author:** ![icottee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3be4f8/32.png) [@icottee](https://www.ruby-forum.com/u/icottee)\
**Post date:** [November 15, 2005, 11:01am UTC](https://www.ruby-forum.com/t/accessing-link-to-from-within-a-controller/47905/2 "2005-11-15T11:01:44Z")

</div>

I’m going to restate my problem as I don’t think I understand what a  
helper is anymore. I now know this.

1. I can’t call link\_to from within a controller because it’s a  
helper. OK. Clear.
2. If I define my menu within a helper, I can call that helper  
directly through my view but not a controller. OK. Clear.
3. I can call paginate from within a controller - even though it’s a  
helper. Hmmm. Not clear.

So what’s the distinction between paginate and link\_to?

Ian

---

<div class="post-metadata">

**Author:** ![icottee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3be4f8/32.png) [@icottee](https://www.ruby-forum.com/u/icottee)\
**Post date:** [November 17, 2005, 10:29am UTC](https://www.ruby-forum.com/t/accessing-link-to-from-within-a-controller/47905/3 "2005-11-17T10:29:22Z")

</div>

Thanks Chris

I took a look but I was trying to avoid creating a partial for each  
menu (which I think this method implies). What I ended up doing was  
keeping my menu definitions in the controller (which I believe is the  
right place for it). It looks something like

def menu  
{:title =\> ‘Vehicles’,  
:items =\> [  
{:title =\> ‘New Vehicle’, :params =\> {:action =\> ‘new’}},  
{:title =\> ‘List Vehicles’, :params =\> {:action =\> ‘list’}}  
]  
}  
end

def menus  
[self.menu]  
end

And then in my views I do a \<% @menus = controller.menus %\>

Finally, my application.rhtml layout looks for @menus to be defined  
and does the necessary to render it to the menu format I want.

Many thanks for the feedback

Ian

---

<div class="post-metadata">

**Author:** ![icottee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3be4f8/32.png) [@icottee](https://www.ruby-forum.com/u/icottee)\
**Post date:** [November 15, 2005, 4:45pm UTC](https://www.ruby-forum.com/t/accessing-link-to-from-within-a-controller/47905/4 "2005-11-15T16:45:08Z")

</div>

you may want to look at render partials

[http://rails.rubyonrails.com/classes/ActionController/Base.html#M000171](http://rails.rubyonrails.com/classes/ActionController/Base.html#M000171)
