Creating routes at run-time

6 views
Skip to first unread message

Nick Hoffman

unread,
Jul 27, 2010, 10:05:44 PM7/27/10
to Ruby on Rails Talk mailing list
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!

Frederick Cheung

unread,
Jul 28, 2010, 3:17:34 AM7/28/10
to Ruby on Rails: Talk
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

Nick

unread,
Jul 28, 2010, 8:49:33 AM7/28/10
to Ruby on Rails: Talk
On Jul 28, 3:17 am, Frederick Cheung <frederick.che...@gmail.com>
wrote:
> 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

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

Nick

unread,
Jul 28, 2010, 9:02:19 AM7/28/10
to Ruby on Rails: Talk
I just found Section 5.4 in http://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!

Nick

unread,
Jul 28, 2010, 10:06:42 PM7/28/10
to Ruby on Rails: Talk
On Jul 28, 9:02 am, Nick <n...@deadorange.com> wrote:
> I just found Section 5.4 inhttp://guides.rubyonrails.org/2_3_release_notes.html
In addition to
ActionController::Routing::RouteSet#add_configuration_file , there's
also #add_named_route , and simply #add_route . How convenient!
Reply all
Reply to author
Forward
0 new messages