我对rails路由感到茫然。 '$ .ajax - > LH:3000 /节点/查询Q =布拉布拉&安培; _ = 1411073392872' 路由到节点#show并且显然在那里失败,因为show需要像/ nodes / 3 /这样的id才能工作。
rake routes
welcome_index GET /welcome/index(.:format) welcome#index
root GET / welcome#index
nodes GET /nodes(.:format) nodes#index
POST /nodes(.:format) nodes#create
new_node GET /nodes/new(.:format) nodes#new
edit_node GET /nodes/:id/edit(.:format) nodes#edit
**node GET /nodes/:id(.:format) nodes#show**
PATCH /nodes/:id(.:format) nodes#update
PUT /nodes/:id(.:format) nodes#update
DELETE /nodes/:id(.:format) nodes#destroy
edges_to_node GET /nodes/:id/toedges(.:format) nodes#searchtoedges
edges_from_node GET /nodes/:id/fromedges(.:format) nodes#searchfromedges
search_edges GET /edges/search(.:format) edges#search
**search_nodes GET /nodes/search(.:format) nodes#search**
在routes.rb中我得到了这个
resources :nodes
resources :edges
get 'nodes/:id/toedges', to: 'nodes#searchtoedges', as: 'edges_to_node'
get 'nodes/:id/fromedges', to: 'nodes#searchfromedges', as: 'edges_from_node'
get '/edges/search', to: 'edges#search', as: 'search_edges'
**get '/nodes/search', to: 'nodes#search', as: 'search_nodes'**
所有其他路线尤其适用于那些搜索路线。如果这很重要,我在ajax调用中使用/ nodes / search。对ajax的响应是一个RecordNotFound异常,抱怨show动作中没有id。因此,由于某种原因,它路由到'/ nodes /:id(。:format)=>节点#show'。我不明白为什么。
答案 0 :(得分:2)
路线从上到下匹配。与/nodes/search
匹配的第一条路线是您的展示路线,其ID为"search"
。
将您的自定义get /nodes/search
路径移至上方<{1}}行,以便resources :nodes
的GET请求与预期路由匹配。
或者,将其添加到资源声明中:
/nodes/search