# Building a Root Route with a Generator

**URL:** <https://www.ruby-forum.com/t/building-a-root-route-with-a-generator/154555>\
**Category:** Rails\
**Created:** [December 1, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/building-a-root-route-with-a-generator/154555 "2008-12-01T16:53:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sinkva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9fc29f/32.png) [@sinkva](https://www.ruby-forum.com/u/sinkva)\
**Post date:** [December 1, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/building-a-root-route-with-a-generator/154555/1 "2008-12-01T16:53:11Z")

</div>

I’m looking for a better approach… then what I’m currently doing.

I’m working on a generator that will help me setup my base projects  
with many of my common defaults that I use internally. Now I know that  
generators have the route\_resources method that allows a person to  
create a resource in the routes file, but is there a way to create the  
map.root :controller =\> ‘name’ entry as well?

Here is what I’ve done. I customized the route\_resources method to be  
something like this:

```
def add_root_route
  sentinel = 'ActionController::Routing::Routes.draw do |map|'
  gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi

```

do |match|  
“#{match}\n map.root :controller =\> “welcome”\n”  
end  
end

And then just copied the gsub\_file method to my generator (it was a  
protected method and I couldn’t think of another way to execute it -  
thoughts?).

I then have a bit that does:

```
  action = File.basename($0)
  case action
    when "generate"
      add_root_route
    when "destroy"
      puts

```

“------------------------------------------------------------”  
puts “Make sure you remove the map.root from the /config/  
routes.rb”  
puts  
“------------------------------------------------------------”  
end

Which makes sure it only runs when they are running generate.

My question is this… Is there an better way to be doing this? It  
seems like a lot of insanity just to add a root route.

---

<div class="post-metadata">

**Author:** ![sinkva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9fc29f/32.png) [@sinkva](https://www.ruby-forum.com/u/sinkva)\
**Post date:** [December 1, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/building-a-root-route-with-a-generator/154555/2 "2008-12-01T18:35:55Z")

</div>

On Mon, Dec 1, 2008 at 10:52 AM, Tim K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My question is this… Is there an better way to be doing this? It  
> seems like a lot of insanity just to add a root route.

You would think so, but there doesn’t seem to be. `script/generate resource` also adds routes, but looking at the source, they’re doing  
something similar:

```
    def route_resources(*resources)
      resource_list = resources.map { |r| r.to_sym.inspect }.join(', 

```

')  
sentinel = ‘ActionController::Routing::Routes.draw do |map|’

```
      logger.route "map.resources #{resource_list}"
      unless options[:pretend]
        gsub_file 'config/routes.rb',

```

/(#{Regexp.escape(sentinel)})/mi do |match|  
“#{match}\n map.resources #{resource\_list}\n”  
end  
end  
end

## Brandon

* * *

Training by Collective Idea: Ruby on Rails training in a vacation  
setting  
[http://training.collectiveidea.com](http://training.collectiveidea.com) – San Antonio, TX – Jan 20-23

---

<div class="post-metadata">

**Author:** ![sinkva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9fc29f/32.png) [@sinkva](https://www.ruby-forum.com/u/sinkva)\
**Post date:** [December 1, 2008, 7:27pm UTC](https://www.ruby-forum.com/t/building-a-root-route-with-a-generator/154555/3 "2008-12-01T19:27:23Z")

</div>

Okay cool. Is there a better way to call gsub\_file protected method  
then by copying it into the generator? It’s in  
Rails::Generator::Commands::Base.
