# Creating routes at run-time

**URL:** <https://www.ruby-forum.com/t/creating-routes-at-run-time/192312>\
**Category:** Rails\
**Created:** [July 28, 2010, 4:07am UTC](https://www.ruby-forum.com/t/creating-routes-at-run-time/192312 "2010-07-28T04:07:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [July 28, 2010, 4:07am UTC](https://www.ruby-forum.com/t/creating-routes-at-run-time/192312/1 "2010-07-28T04:07:11Z")

</div>

Hey guys. In Rails 2, is there a way to create a route at run-time? I  
want to do this because a gem that I’m writing requires a single  
resource route, and I’d prefer that the gem create it at run-time  
instead of forcing the user to define the route in routes.rb .

While trying to figure this out, I discovered that calling  
ActionController::Routing::Routes.draw do |map|  
# my gem’s route here  
end  
causes all of the routes in routes.rb to be removed.

I tried copying the routes from ActionController::Routing::Routes into  
a new array or hash, then rebuilding them and adding my gem’s route,  
but couldn’t get that to work. Also, it’s messy and less than ideal.

So, any suggestions? I’m all ears!

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [July 28, 2010, 9:22am UTC](https://www.ruby-forum.com/t/creating-routes-at-run-time/192312/2 "2010-07-28T09:22:05Z")

</div>

On Jul 28, 3:05 am, Nick H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I tried copying the routes from ActionController::Routing::Routes into  
> a new array or hash, then rebuilding them and adding my gem’s route,  
> but couldn’t get that to work. Also, it’s messy and less than ideal.

If you have a plugin with a structure that looks like

my\_plugin/  
app/  
controllers/  
…

config/  
routes.rb  
lib/  
…

Then rails will add the routes from that routes.rb file. (Don’t know  
if you have to have app/controllers, i’ve just always wanted to add a  
route to a controller that was also in the plugin).

Since plugins can be packaged as gems, some variation on the above  
should work

Fred

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [July 28, 2010, 2:51pm UTC](https://www.ruby-forum.com/t/creating-routes-at-run-time/192312/3 "2010-07-28T14:51:07Z")

</div>

On Jul 28, 3:17 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> ...
> 
> ```
> 
> Then rails will add the routes from that routes.rb file. (Don’t know  
> if you have to have app/controllers, i’ve just always wanted to add a  
> route to a controller that was also in the plugin).
> 
> Since plugins can be packaged as gems, some variation on the above  
> should work
> 
> Fred

Interesting! I didn’t that you could do that. Unfortunatey, I couldn’t  
get it to work.

$cat config/routes.rb  
ActionController::Routing::Routes.draw do |map|  
map.resource :dashboard, :only =\> :show  
map.connect “dashboard/widgets/_path", :controller  
=\> :dashboards, :action =\> ‘widget\_data’  
end  
$  
$ cat vendor/gems/acts\_as\_dashboard-0.0.0/config/routes.rb  
puts "_\*\*\*\* #{ **FILE** }”  
ActionController::Routing::Routes.draw do |map|  
map.connect ‘foo/\*path’, :controller =\> :dashboards  
end  
$  
$ rake routes  
(in /home/nick/src/acts\_as\_dashboard\_container)  
dashboard GET /dashboard(.:format)  
{:action=\>“show”, :controller=\>“dashboards”}  
/dashboard/widgets/:path  
{:action=\>“widget\_data”, :controller=\>“dashboards”}  
$

Any idea why the gem’s routes.rb isn’t being processed?

Thanks!  
Nick

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [July 28, 2010, 3:04pm UTC](https://www.ruby-forum.com/t/creating-routes-at-run-time/192312/4 "2010-07-28T15:04:00Z")

</div>

I just found Section 5.4 in

> **[Ruby on Rails 2.3 Release Notes — Ruby on Rails Guides](https://guides.rubyonrails.org/2_3_release_notes.html)**
>
> Ruby on Rails 2.3 Release NotesRails 2.3 delivers a variety of new and improved features, including pervasive Rack integration, refreshed support for Rails Engines, nested transactions for Active Record, dynamic and default scopes, unified rendering,...

:

“Another big change is that Rails now supports multiple routing files,  
not just routes.rb. You can use RouteSet#add\_configuration\_file to  
bring in more routes at any time – without clearing the currently-  
loaded routes.”

I’ll check this out tonight!

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [July 29, 2010, 4:07am UTC](https://www.ruby-forum.com/t/creating-routes-at-run-time/192312/5 "2010-07-29T04:07:55Z")

</div>

On Jul 28, 9:02 am, Nick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I just found Section 5.4 inhttp://guides.rubyonrails.org/2\_3\_release\_notes.html  
> :
> 
> “Another big change is that Rails now supports multiple routing files,  
> not just routes.rb. You can use RouteSet#add\_configuration\_file to  
> bring in more routes at any time – without clearing the currently-  
> loaded routes.”
> 
> I’ll check this out tonight!

In addition to  
ActionController::Routing::RouteSet#add\_configuration\_file , there’s  
also #add\_named\_route , and simply #add\_route . How convenient!
