I have a menu_items array (array of hashes, with printed name and url
for each item) in a helper, which i use to create my mainmenu. I’d
like to use them in routes.rb so i can loop through them and map
routes according to their names. Is there a way to do so? Or should i
set this up in a different way?
You might be able to do this with at least one of change. Since the
essence of routing is matching a (named) url with the controller/
action that needs to respond to it, you’ll need to add info to your
hash. Armed with that you might be able to do something like
(untested):
MenuItemHelper::MENU.each do |menu_mapping|
map.send menu_mapping[:name],
menu_mapping[:url], :controller=>menu_mapping[:controller],
:action=>menu_mapping[:action]
end
HTH,
AndyV
So that would be helperclass::methodname.each to get access to the
correct array. Thank you, that’s what i needed. But what exactly does
map.send do here? i assume this command translates for each item into
something like 'map.connect ‘“action”, :controller => etc etc’ ? or
does map.send do something xompletely different? (i don’t see the
‘connect’ in your example)
Have a look at this episode from Railscasts. I’ve used this many
times now to use a lot of my ‘brochureware’ style pages that aren’t
really resources that fit inside the REST ideas. I usually create one
controller to deal with all that static content and then use this idea
from railscasts to just map a whole bunch of them with named routes.
hope this helps: