# DRYing Routes

**URL:** <https://www.ruby-forum.com/t/drying-routes/191065>\
**Category:** Rails\
**Created:** [July 4, 2010, 4:38am UTC](https://www.ruby-forum.com/t/drying-routes/191065 "2010-07-04T04:38:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwormuth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dwormuth](https://www.ruby-forum.com/u/dwormuth)\
**Post date:** [July 4, 2010, 4:38am UTC](https://www.ruby-forum.com/t/drying-routes/191065/1 "2010-07-04T04:38:39Z")

</div>

I have a series of ‘static’ pages for a site that need named routes in  
Rails 3. I’m trying to DRY the routes file, but can’t seem to find the  
proper code.

The desired output/code is:

match ‘/about’ =\> ‘info#about’, :as =\> :about

for each page. My attempt to dry this:

%w(about services articles marketing clients).each do |page|  
match eval( “’/#{page}’ =\> ‘info##{page}’, :as =\> :#{page}”)  
end

Any suggestions?

---

<div class="post-metadata">

**Author:** ![dwormuth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dwormuth](https://www.ruby-forum.com/u/dwormuth)\
**Post date:** [July 4, 2010, 4:45am UTC](https://www.ruby-forum.com/t/drying-routes/191065/2 "2010-07-04T04:45:05Z")

</div>

I figured this out:

```
eval( "match '/#{page}' => 'info##{page}', :as => :#{page}")
```

---

<div class="post-metadata">

**Author:** ![dwormuth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dwormuth](https://www.ruby-forum.com/u/dwormuth)\
**Post date:** [July 4, 2010, 8:28am UTC](https://www.ruby-forum.com/t/drying-routes/191065/3 "2010-07-04T08:28:49Z")

</div>

dwormuth wrote:

> I have a series of ‘static’ pages for a site that need named routes in  
> Rails 3. I’m trying to DRY the routes file, but can’t seem to find the  
> proper code.  
> Â  
> The desired output/code is:  
> Â  
> match ‘/about’ =\> ‘info#about’, :as =\> :about  
> Â  
> for each page. My attempt to dry this:  
> Â  
> %w(about services articles marketing clients).each do |page|  
> Â Â match eval( “‘/#{page}’ =\> ‘info##{page}’, :as =\> :#{page}”)  
> end

eval is almost never a good idea, and it certainly isn’t needed here.  
Try:

match “/#{page}” =\> “info##{page}”, :as =\> page.to\_symÂ

> Â  
> Any suggestions?

Remember to try string interpolation and send. Â eval is not that useful  
in Ruby – if you think you need it, remember that you almost certainly  
don’t.Â

–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

Sent from my iPhone
