问题描述
我在 routes.rb 文件中设置了以下路由:
I have the following routes set up in my routes.rb file:
resources :people do
collection do
get :search
end
end
当我对 url 执行 get 操作时:http://localhost:3000/people/search.json?term=stepeb,服务器报告它正在响应 show 操作,使用正确的 term 参数,但还有一个 id 参数,设置为搜索".
When i do a get action on the url: http://localhost:3000/people/search.json?term=stepeb,the server reports that it's responding with the show action, with the correct term parameter, but also has an id parameter, set to "search".
在我看来,问题是两个网址节目网址将是:
The problem, as i see it, are the two urlsthe show url would be:
/people/:id
并且我相信路由器在到达/people/search 之前匹配该路由
and i believe that the router is matching that route before it gets to /people/search
如果是这样,基于集合的路由将如何工作?他们不会都被表演动作抓住了吗?
If that is the case, how would collection based routes ever work? Wouldnt they all get caught by the show action?
rake 路由相关部分如下:
The relevant part of rake routes is as follows:
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"}
推荐答案
哦,忘了这个吧.结果我在路由文件的顶部有一个重复的资源 :people 行.Rails 首先实现了这一目标.在我看来,真的应该检查那里是否有重复的路由定义.
Doh, forget this one. Turns out i had a duplicate resources :people line at the top of the routes file. Rails was hitting that first. Seems to me there really should be a check for duplicate route definition in there.
这篇关于Rails 忽略收集路线并改为使用 show 动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!