# Problems with defining routes

**URL:** <https://www.ruby-forum.com/t/problems-with-defining-routes/125451>\
**Category:** Rails\
**Created:** [January 9, 2008, 7:11am UTC](https://www.ruby-forum.com/t/problems-with-defining-routes/125451 "2008-01-09T07:11:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 9, 2008, 7:11am UTC](https://www.ruby-forum.com/t/problems-with-defining-routes/125451/1 "2008-01-09T07:11:32Z")

</div>

hey i am having problems when defining routes like this.

map.room ‘room/:name’, :controller =\> ‘room’, :action =\> ‘show’

when i try goto any url such as:  
/room/123  
/room/technology

it works fine to goto the show method with a param :name.

but when i want to use my other actions such as ‘list’ it still treats  
it as room/:name and will direct it to my show action.

eg.  
room/list gives

\*\*NoMethodError in Room#show

Showing room/show.rhtml where line #1 raised:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.name

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 9, 2008, 7:13am UTC](https://www.ruby-forum.com/t/problems-with-defining-routes/125451/2 "2008-01-09T07:13:17Z")

</div>

Try map.resources :room instead.

This will use restful routing which will do exactly what you want it to  
do,  
but instead list is called “index”.

On Jan 9, 2008 4:40 PM, Chubbs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.name
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 9, 2008, 7:17am UTC](https://www.ruby-forum.com/t/problems-with-defining-routes/125451/3 "2008-01-09T07:17:23Z")

</div>

mmm but how do i create the route for /room/technology?

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 9, 2008, 7:30am UTC](https://www.ruby-forum.com/t/problems-with-defining-routes/125451/4 "2008-01-09T07:30:15Z")

</div>

im a bit confused

im just wanting to call the show action for a name for the room.

eg,  
/room/technology  
should goto /room/show with param[:name] = technology,  
so i can show the subtopics within technology

would i have to create a maps.resources for each of my room names ?

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 9, 2008, 7:33am UTC](https://www.ruby-forum.com/t/problems-with-defining-routes/125451/5 "2008-01-09T07:33:10Z")

</div>

No, map.resources :rooms will automatically define the routes you need  
for  
CRUD’ding resources: index, show, new, create, edit, update & delete.

This means when you do /rooms/technology it’ll actually pass in  
“technology”  
as params[:id], not params[:name]. Just a minor alteration in your code  
should fix this.

On Jan 9, 2008 4:59 PM, Chubbs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> would i have to create a maps.resources for each of my room names ?
> 
> > Whether you do :member or :collection determines whether you do  
> > On Jan 9, 2008 4:46 PM, Chubbs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > > This will use restful routing which will do exactly what you want it
> > > > 
> > > > > when i try goto any url such as:  
> > > > > room/list gives  
> > > > > Feel free to add me to MSN and/or GTalk as this email.
> > 
> > –  
> > Ryan B.[http://www.frozenplague.net](http://www.frozenplague.net)  
> > Feel free to add me to MSN and/or GTalk as this email.

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 9, 2008, 7:39am UTC](https://www.ruby-forum.com/t/problems-with-defining-routes/125451/6 "2008-01-09T07:39:36Z")

</div>

ahh i get you,i change it and got it working

ok ill start using RESTful routing from now on,

thanks for the advice

-chubbs

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 9, 2008, 9:31am UTC](https://www.ruby-forum.com/t/problems-with-defining-routes/125451/7 "2008-01-09T09:31:24Z")

</div>

On Jan 9, 2008, at 7:29 AM, Chubbs wrote:

> im just wanting to call the show action for a name for the room.
> 
> eg,  
> /room/technology  
> should goto /room/show with param[:name] = technology,  
> so i can show the subtopics within technology
> 
> would i have to create a maps.resources for each of my room names ?

Just another detail: you can still have the room name in the URL with  
a custom Room#to\_param that returns the name, or the trick “#{id}-  
name” you can see out there. That parameter goes to params[:id] anyway.

– fxn

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 9, 2008, 7:20am UTC](https://www.ruby-forum.com/t/problems-with-defining-routes/125451/8 "2008-01-09T07:20:54Z")

</div>

Ahh, the wonders of restful routing elude you:

map.resources :room, :member =\> { :technology =\> :get }

Or if you’re doing it for a whole bunch of rooms:

map.resources :room, :collection =\> { :technology =\> :get }

Whether you do :member or :collection determines whether you do  
technology\_room\_path(@room) or technology\_rooms\_path(@room).

I covered restful routing a few days ago on this list and wrote a  
smallish  
tutorial for it: [frozenplague.net](http://frozenplague.net/?p=108)

If you have any questions do not hesitate to contact me through the list  
or  
through GTalk or MSN at this email.

On Jan 9, 2008 4:46 PM, Chubbs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > when i try goto any url such as:

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.
