Forum: Ruby on Rails Accessing link_to from within a controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
icottee (Guest)
on 2005-11-15 02:14
(Received via mailing list)
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
icottee (Guest)
on 2005-11-15 12:01
(Received via mailing list)
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
christopher.k.hall (Guest)
on 2005-11-15 17:45
(Received via mailing list)
you may want to look at render partials

http://rails.rubyonrails.com/classes/ActionControl...
icottee (Guest)
on 2005-11-17 11:29
(Received via mailing list)
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
This topic is locked and can not be replied to.