# Map routes from an array?

**URL:** <https://www.ruby-forum.com/t/map-routes-from-an-array/129082>\
**Category:** Rails\
**Created:** [February 14, 2008, 2:03am UTC](https://www.ruby-forum.com/t/map-routes-from-an-array/129082 "2008-02-14T02:03:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 14, 2008, 2:03am UTC](https://www.ruby-forum.com/t/map-routes-from-an-array/129082/1 "2008-02-14T02:03:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 14, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/map-routes-from-an-array/129082/2 "2008-02-14T21:41:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 17, 2008, 10:43am UTC](https://www.ruby-forum.com/t/map-routes-from-an-array/129082/3 "2008-02-17T10:43:34Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 18, 2008, 12:12am UTC](https://www.ruby-forum.com/t/map-routes-from-an-array/129082/4 "2008-02-18T00:12:04Z")

</div>

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:

> **[\#79 Generate Named Routes - RailsCasts](http://railscasts.com/episodes/79-generate-named-routes)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
