Rails忽略了收集路由,而是与show action一起使用

我在routes.rb文件中设置了以下路由:

resources :people do collection do get :search end end 

当我对url执行get操作时: http:// localhost:3000 / people / search.json?term = stepeb ,服务器报告它正在响应show动作,使用正确的term参数,但也有一个id参数,设置为“搜索”。

正如我所看到的,问题是显示url的两个url:

 /people/:id 

我相信路由器在进入/ people / search之前匹配该路由

如果是这样的话,基于收集的路线将如何运作? 他们都不会受到节目动作的影响吗?

耙路线的相关部分如下:

  search_people GET /people/search(.:format) {:action=>"search", :controller=>"people"} GET /people(.:format) {:action=>"index", :controller=>"people"} people POST /people(.:format) {:action=>"create", :controller=>"people"} new_person GET /people/new(.:format) {:action=>"new", :controller=>"people"} GET /people/:id(.:format) {:action=>"show", :controller=>"people"} PUT /people/:id(.:format) {:action=>"update", :controller=>"people"} person DELETE /people/:id(.:format) {:action=>"destroy", :controller=>"people"} edit_person GET /people/:id/edit(.:format) {:action=>"edit", :controller=>"people"} 

Doh,别忘了这个。 事实certificate我有一个重复的资源:人们排在路线文件的顶部。 Rails首先击中了它。 在我看来,确实应该在那里检查重复的路线定义。

你在运行什么版本的Rails? 尝试使用您在此处提供的代码创建测试应用程序,看看它是否有效。 可能还有其他一些你没有提到过的冲突。

在Rails 3.0.0beta4上使用您的代码产生了预期的结果:

 Started GET "/people/search.json?term=boo" for 192.168.1.2 at 2010-06-23 03:39:26 -0400 Processing by PeopleController#search as JSON Parameters: {"term"=>"boo"} Completed in 49ms 

这是我的路线文件:

  resources :people do collection do get :search end end 

我有一个people_controller.rb,定义了搜索方法。

我也有类似的问题。 根据你的例子,我的routes.rb看起来像这样

 resources :people ... resources :people do collection do get :search end end 

将其更改为:

 resources :people do collection do get :search end end ... resources :people 

我可以访问该集合…顺便说一句,这是添加路由的适当方式吗? 即,在向控制器添加动作并留下“旧”资源时添加新路径是一种好方式:人们喜欢它吗?