# Rails 1.2 routes question

**URL:** <https://www.ruby-forum.com/t/rails-1-2-routes-question/85894>\
**Category:** Rails\
**Created:** [February 17, 2007, 11:45am UTC](https://www.ruby-forum.com/t/rails-1-2-routes-question/85894 "2007-02-17T11:45:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [February 17, 2007, 11:45am UTC](https://www.ruby-forum.com/t/rails-1-2-routes-question/85894/1 "2007-02-17T11:45:37Z")

</div>

Hi,

I am getting to grips with Raisl 1.2 and am using the new restfull  
features. I have my model article and used scaffold\_resource article to  
geive me the restfull default crud methods. So in the routes.rb i have

map.resources :articles

in the articles controller i want to add a custom method lets say list  
and have a list view to show all the articles in the model.

def list  
@articles = Article.find(:all)  
end

What is the best way to then access the list method in the articles  
controller. If i try [http://localhost:3004/articles/list](http://localhost:3004/articles/list) i get an  
error. Do i need to set up a named route or can i add something to the  
map.resources :articles to let it know to accept requests for the list  
action?

Is using map.connect :controller/:action/:id, :controller =‘xxxx’,  
:action =‘’ not good practice in this version?

Any advice appreciated

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [February 17, 2007, 3:38pm UTC](https://www.ruby-forum.com/t/rails-1-2-routes-question/85894/2 "2007-02-17T15:38:34Z")

</div>

1. Why not just use the already declared index method?

2. If you really have to have an additional list method, just add

:collection =\> {:list =\> :get} # Presuming you’re gonna use GET to “get”  
the  
page/resource

to your mapping for that resource.

But I’d seriously question the need for both an index and a list method.  
That’s where people are going to land normally based on the common  
assumptions that most routing uses. [http://domain.com/controller](http://domain.com/controller) ==  
[http://domain.com/controller/index](http://domain.com/controller/index). So you’re either going to risk  
reduplication [index == list] or redirection. Either way sounds like  
unnecessary work. Yuck.

I _can_ see cases where you might want to use this methodology of a  
separate  
list method but not for regular cases. And _certainly_ not just because  
the  
old scaffolding used that method. Ick. Scaffolding.

Excuse my snottiness. I have a cold. 😉

RSL

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [February 19, 2007, 1:11pm UTC](https://www.ruby-forum.com/t/rails-1-2-routes-question/85894/3 "2007-02-19T13:11:54Z")

</div>

Russell N. wrote:

> 1. Why not just use the already declared index method?
> 
> 2. If you really have to have an additional list method, just add
> 
> :collection =\> {:list =\> :get} # Presuming you’re gonna use GET to “get”  
> the  
> page/resource
> 
> to your mapping for that resource.
> 
> But I’d seriously question the need for both an index and a list method.  
> That’s where people are going to land normally based on the common  
> assumptions that most routing uses. [http://domain.com/controller](http://domain.com/controller) ==  
> [http://domain.com/controller/index](http://domain.com/controller/index). So you’re either going to risk  
> reduplication [index == list] or redirection. Either way sounds like  
> unnecessary work. Yuck.
> 
> I _can_ see cases where you might want to use this methodology of a  
> separate  
> list method but not for regular cases. And _certainly_ not just because  
> the  
> old scaffolding used that method. Ick. Scaffolding.
> 
> Excuse my snottiness. I have a cold. 😉
> 
> RSL

Thanks for your reply. I was using list as an example but basically i  
want to know how to add a another action to a controller that has the  
default CRUD actions from using scaffold\_resource . What do i put in  
the routes.rb file as i already have:

map.resources :articles

So if i had a action called ‘list’ then what is the best way to let  
routes.rb know to except requests of [http://localhost:3004/articles/list](http://localhost:3004/articles/list)  
and what do i put in a view to link th this action. Its an ordinary GET  
request so i notice similar to edit\_article\_path have a  
list\_article\_path.

Is this even possible or do i create a named route for each extra action  
that is on the controller?

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [February 19, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/rails-1-2-routes-question/85894/4 "2007-02-19T16:17:59Z")

</div>

> So if i had a action called ‘list’ then what is the best way to let  
> routes.rb know to except requests of [http://localhost:3004/articles/list](http://localhost:3004/articles/list)

Using REST, you can write the url:  
http://localhost:3004articles and that will invoke the index method,  
which would be your list method.

To create a link from a view, you would use:  
articles\_path

And in your routes.rb file you’d declare each RESTful controller:  
map.resources :articles as you are already, and add a new declaration  
for each controller.

I found the tutorial as Peep Code very useful.

If you still want to use the /articles/list url, you would set the  
route something like:  
map.connect ‘:controller/:action/’

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [February 19, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/rails-1-2-routes-question/85894/5 "2007-02-19T16:36:00Z")

</div>

toby privett wrote:

> > So if i had a action called ‘list’ then what is the best way to let  
> > routes.rb know to except requests of [http://localhost:3004/articles/list](http://localhost:3004/articles/list)
> 
> Using REST, you can write the url:  
> http://localhost:3004articles and that will invoke the index method,  
> which would be your list method.
> 
> To create a link from a view, you would use:  
> articles\_path
> 
> And in your routes.rb file you’d declare each RESTful controller:  
> map.resources :articles as you are already, and add a new declaration  
> for each controller.
> 
> I found the tutorial as Peep Code very useful.
> 
> If you still want to use the /articles/list url, you would set the  
> route something like:  
> map.connect ‘:controller/:action/’

I think using list as an example is confusing things. I know about the  
default routes you get with map.resources :articles and the paths to  
access these routes like edit\_article\_path and that index should be used  
for the list the items.

Say i wanted an action called search on this articles controller so in  
the view i have a file search.rhtml What is the best way for me to  
serve up this file, can i add it to the existing map.resources :articles  
by adding something that was mentioned in the first reply :collection =\>  
{:search =\> :get} , do i create a named route and place that before  
map.resources: articles or do i have a map.connect  
':controller/:action/ like in rails 1.6

Sorry for the confusion, i know about the defaults urls and how they  
work its adding more actions to a controller that already has the  
default CRUD actions that im looking for advice on.

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [February 19, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/rails-1-2-routes-question/85894/6 "2007-02-19T16:45:03Z")

</div>

using :collection =\> { :search =\> :get } in your map.resource  
declaration in the routes.rb file is the correct way to add custom  
actions to your routes.

Like this:

map.resources :zip\_codes, :collection =\> { :search =\> :get }

Controller:

# GET /zip\_codes;search

# GET /zip\_codes.xml;search

def search  
end

The URL would be something like:  
[http://domain.com/articles;search](http://domain.com/articles;search)

Just like using some of the standard resource routes like for edit:  
[http://domain.com/articles;edit](http://domain.com/articles;edit)

On Feb 19, 10:36 am, John B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [February 19, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/rails-1-2-routes-question/85894/7 "2007-02-19T16:52:04Z")

</div>

By the way there are also ways to add routes for singular and new  
resources like this:

Singular route:  
map.resources :articles, :member =\> { :embargo =\> :put, :release  
=\> :put }

New Route:  
map.resources :articles, :new =\> { :shortform =\> :post }

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [February 19, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/rails-1-2-routes-question/85894/8 "2007-02-19T17:14:56Z")

</div>

Robert W. wrote:

> By the way there are also ways to add routes for singular and new  
> resources like this:
> 
> Singular route:  
> map.resources :articles, :member =\> { :embargo =\> :put, :release  
> =\> :put }
> 
> New Route:  
> map.resources :articles, :new =\> { :shortform =\> :post }

And what is the best way to access this new route in the view:

\<%= link\_to “search”, :controller=\>“articles”, :action=\>“search” %\>

or can you do something like search\_articles\_path ???

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [February 19, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/rails-1-2-routes-question/85894/9 "2007-02-19T17:26:13Z")

</div>

Sorry, I forgot that part.

Do so like this:  
\<%= link\_to search\_articles\_path %\>

On Feb 19, 11:14 am, John B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
