# Trouble understanding Routes

**URL:** <https://www.ruby-forum.com/t/trouble-understanding-routes/173019>\
**Category:** Rails\
**Created:** [July 31, 2009, 12:52pm UTC](https://www.ruby-forum.com/t/trouble-understanding-routes/173019 "2009-07-31T12:52:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [July 31, 2009, 12:52pm UTC](https://www.ruby-forum.com/t/trouble-understanding-routes/173019/1 "2009-07-31T12:52:18Z")

</div>

Hey Everyone,  
I’m new to Rails and working through the book Simply Rails 2. I’ve  
just hit one of my first large snags.

This _was_ my routes file:

map.resources :stories, :has\_many =\> :votes, :collection =\> { :bin  
=\> :get }  
map.connect ‘:controller/:action/:id’  
map.connect ‘:controller/:action/:id.:format’  
map.resource :session  
map.root :controller =\> ‘stories’  
map.resources :users

my problem was anytime I traveled to the url /users/1-username/ i got  
an error saying the only action was ‘show’  
Which did and did not make sense to me. It did make sense becuase the  
only action in my controller was show. and in both theory (and I tried  
it and it worked) the url should be /users/show/1-username. Which  
worked without a problem. But of course the book only user the /users/  
1-username/ url.

So after some time i changed my routes file to:

map.resources :users  
map.resources :stories, :has\_many =\> :votes, :collection =\> { :bin  
=\> :get }  
map.connect ‘:controller/:action/:id’  
map.connect ‘:controller/:action/:id.:format’  
map.resource :session  
map.root :controller =\> ‘stories’

Which fixed my problem. I can now access /users/1-username/ as  
expected (displays user information).

But I really don’t understand why. Why is it now the controller is  
defaulting to it’s only action but before it was not?

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [July 31, 2009, 2:24pm UTC](https://www.ruby-forum.com/t/trouble-understanding-routes/173019/2 "2009-07-31T14:24:10Z")

</div>

Hi brianp

```
 Your default routes should have the least priority Means

```

map.connect ‘:controller/:action/:id’  
map.connect ‘:controller/:action/:id.:format’

These should be at the very bottom of routes.rb

Sijo

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [July 31, 2009, 4:51pm UTC](https://www.ruby-forum.com/t/trouble-understanding-routes/173019/3 "2009-07-31T16:51:38Z")

</div>

map.resources

This is the same thing as stating that you have a RESTful controller  
setup and that you have paths in place for all 7 controller methods  
(index, show, new, edit, create, update, and destroy).

map.connect ‘:controller/:action/:id’  
map.connect ‘:controller/:action/:id.:format’

This allows you to have urls in place for…

[yourdomain.com is available for purchase - Sedo.com](http://yourdomain.com/controller/action/id) OR  
[yourdomain.com is available for purchase - Sedo.com](http://yourdomain.com/controller/id/action) etc.

If you haven’t modified how your routing and paths are going to behave  
in each of your controllers then you want to make sure those two lines  
are always at the very bottom of the your routes file. Routes have  
precedence in place for their order.

If you want to use javascript in your routes you would place something  
like this:

map.js ‘:controller/:action.:format’

… at the very bottom as well.

If you wanted a catchall routes type of path set you can set the  
following at the bottom:

map.connect “\*anything”, :controller =\> ‘controllername’, :action =\>  
‘request\_error’

… just an example …

You should check out:

> **[Rails Routing from the Outside In — Ruby on Rails Guides](https://guides.rubyonrails.org/routing.html)**
>
> Rails Routing from the Outside InThis guide covers the user-facing features of Rails routing.After reading this guide, you will know: How to interpret the code in config/routes.rb. How to construct your own routes, using either the preferred...

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [July 31, 2009, 8:21pm UTC](https://www.ruby-forum.com/t/trouble-understanding-routes/173019/4 "2009-07-31T20:21:16Z")

</div>

Sijo,

That is not necessarily good advice. You don’t always need (or want)  
these default routes.

Even the comments produced automatically when creating a new Rails app  
tell you this:

# Install the default routes as the lowest priority.

# …You should consider removing them or commenting them out if

you’re using named routes and resources.

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [August 3, 2009, 6:15am UTC](https://www.ruby-forum.com/t/trouble-understanding-routes/173019/5 "2009-08-03T06:15:20Z")

</div>

E. Litwin wrote:

> Sijo,
> 
> That is not necessarily good advice. You don’t always need (or want)  
> these default routes.
> 
> Even the comments produced automatically when creating a new Rails app  
> tell you this:
> 
> # Install the default routes as the lowest priority.
> 
> # …You should consider removing them or commenting them out if
> 
> you’re using named routes and resources.

```
Yes that is right. Sorry I forgot to tell that

```

Sijo

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [July 31, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/trouble-understanding-routes/173019/6 "2009-07-31T18:45:15Z")

</div>

Thanks for the responses. I think I understand it now so I’ll recap  
real quick

// Routes get written in a priority order. Specialized routes on the  
top (in this case assuming a RESTful manner from the controllers)  
map.resources :users  
map.resource :session  
map.resources :stories, :has\_many =\> :votes, :collection =\> { :bin  
=\> :get }

// And the default layout for all routes not specifically set above  
map.root :controller =\> “stories”  
map.connect ‘:controller/:action/:id’  
map.connect ‘:controller/:action/:id.:format’

Thanks again,  
brianp

On Jul 31, 7:51 am, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
