# Add a restful route

**URL:** <https://www.ruby-forum.com/t/add-a-restful-route/112551>\
**Category:** Rails\
**Created:** [August 20, 2007, 8:53pm UTC](https://www.ruby-forum.com/t/add-a-restful-route/112551 "2007-08-20T20:53:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 20, 2007, 8:53pm UTC](https://www.ruby-forum.com/t/add-a-restful-route/112551/1 "2007-08-20T20:53:35Z")

</div>

In using the restful2 example in Agile Development with Rails I thought  
it would be good to have a search function on title. So I added the  
search function to the controller with the other CRUD funtions. Added  
an input field to the index.rhtml page. I get the error “no route found  
‘/search’”. And have not be able to come up with the syntax to add the  
route. Did I add the function to wrong place or how do you create the  
route?

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 20, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/add-a-restful-route/112551/2 "2007-08-20T20:56:27Z")

</div>

David Allen wrote:

> In using the restful2 example in Agile Development with Rails I thought  
> it would be good to have a search function on title. So I added the  
> search function to the controller with the other CRUD funtions. Added  
> an input field to the index.rhtml page. I get the error “no route found  
> ‘/search’”. And have not be able to come up with the syntax to add the  
> route. Did I add the function to wrong place or how do you create the  
> route?

Might help if you posted your routes so we can see what chew talking  
about…

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 20, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/add-a-restful-route/112551/3 "2007-08-20T21:49:03Z")

</div>

> ActionController::Routing::Routes.draw do |map|  
> map.resources :articles do |article|  
> article.resources :comments  
> end

see what happens when u change the above to:

map.resources :articles do |article|  
article.resources :comments  
article.resources :search  
end

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 20, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/add-a-restful-route/112551/4 "2007-08-20T21:25:02Z")

</div>

Dave C. wrote:

> David Allen wrote:
> 
> > In using the restful2 example in Agile Development with Rails I thought  
> > it would be good to have a search function on title. So I added the  
> > search function to the controller with the other CRUD funtions. Added  
> > an input field to the index.rhtml page. I get the error “no route found  
> > ‘/search’”. And have not be able to come up with the syntax to add the  
> > route. Did I add the function to wrong place or how do you create the  
> > route?
> 
> Might help if you posted your routes so we can see what chew talking  
> about…

routes.rb:  
#—

# Excerpted from “Agile Web D. with Rails, 2nd Ed.”

# We make no guarantees that this code is fit for any purpose.

# Visit [http://www.pragmaticprogrammer.com/titles/rails2](http://www.pragmaticprogrammer.com/titles/rails2) for more book

information.  
#—

ActionController::Routing::Routes.draw do |map|  
map.resources :articles do |article|  
article.resources :comments  
end

And the serarch function in articles\_controller.rb:

# GET /articles/l

# GET /articles/l.xml

def search  
if params[:Ndc]  
@article = Article.find(:all, conditions =\> [“title like ?”,  
“%#{params[:Ndc]}%”]).each  
else  
@artivle = []  
end

```
respond_to do |format|
  format.html # show.rhtml
  format.xml { render :xml => @article.to_xml }
end

```

end

end

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 20, 2007, 10:02pm UTC](https://www.ruby-forum.com/t/add-a-restful-route/112551/5 "2007-08-20T22:02:42Z")

</div>

Dave C. wrote:

> > see what happens when u change the above to:
> > 
> > map.resources :articles do |article|  
> > article.resources :comments  
> > article.resources :search  
> > end
> 
> ok. if was actually thinking I wouldn’t have typed that 🙂
> 
> sorry, ignore the previous post.
> 
> try:
> 
> map.resources :articles, :collection =\> { :search =\> :get } do  
> |article|  
> article.resources :comments  
> end
> 
> Hopefully im right this time…

Yes, I did try that and dropped off the comments. Got an error that  
collection was not defined. This is where I cannot find any good docs  
on how the routes work.

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 20, 2007, 9:54pm UTC](https://www.ruby-forum.com/t/add-a-restful-route/112551/6 "2007-08-20T21:54:42Z")

</div>

> see what happens when u change the above to:
> 
> map.resources :articles do |article|  
> article.resources :comments  
> article.resources :search  
> end

ok. if was actually thinking I wouldn’t have typed that 🙂

sorry, ignore the previous post.

try:

map.resources :articles, :collection =\> { :search =\> :get } do  
|article|  
article.resources :comments  
end

Hopefully im right this time…

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 20, 2007, 10:07pm UTC](https://www.ruby-forum.com/t/add-a-restful-route/112551/7 "2007-08-20T22:07:38Z")

</div>

> Yes, I did try that and dropped off the comments. Got an error that  
> collection was not defined. This is where I cannot find any good docs  
> on how the routes work.

[http://peepcode.com/products/restful-rails](http://peepcode.com/products/restful-rails)  
and

> **[REST-cheatsheet.pdf](https://topfunky.com/clients/peepcode/REST-cheatsheet.pdf)**
>
> 78.73 KB

might help.

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 20, 2007, 10:08pm UTC](https://www.ruby-forum.com/t/add-a-restful-route/112551/8 "2007-08-20T22:08:33Z")

</div>

> Yes, I did try that and dropped off the comments. Got an error that  
> collection was not defined. This is where I cannot find any good docs  
> on how the routes work.

and …

> **[\#35 Custom REST Actions - RailsCasts](http://railscasts.com/episodes/35-custom-rest-actions)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
