Add a restful route

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?

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…

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

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 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

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 :slight_smile:

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.

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 :slight_smile:

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.

http://peepcode.com/products/restful-rails
and

might help.

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 …