Rails:路由和路径助手

可能重复:
Rails:没有复数的路由给出了奇怪的帮助

结果我有:资源qtl_table在config / routes.rb两次! 我收到此错误:

undefined local variable or method `search_qtl_table_index' for #<#:0x805af47b0> 

在app / views / qtl_table / index.html.erb中:

 

Search for QTL

'get' do %> nil %>

在config / routes.rb中:

 Qtl::Application.routes.draw do resources :qtl_table do collection do get 'search' end end ... end 

我确实关闭了复数:

 ActiveRecord::Base.pluralize_table_names = false 

耙路线的输出:

  search_qtl_table_index GET /qtl_table/search(.:format) {:action=>"search", :controller=>"qtl_table"} qtl_table_index GET /qtl_table(.:format) {:action=>"index", :controller=>"qtl_table"} POST /qtl_table(.:format) {:action=>"create", :controller=>"qtl_table"} new_qtl_table GET /qtl_table/new(.:format) {:action=>"new", :controller=>"qtl_table"} edit_qtl_table GET /qtl_table/:id/edit(.:format) {:action=>"edit", :controller=>"qtl_table"} qtl_table GET /qtl_table/:id(.:format) {:action=>"show", :controller=>"qtl_table"} PUT /qtl_table/:id(.:format) {:action=>"update", :controller=>"qtl_table"} DELETE /qtl_table/:id(.:format) {:action=>"destroy", :controller=>"qtl_table"} 

您可能关闭了复数,但这只会影响数据库中的表名,而不会影响Rails处理路由的方式。

因为search路径属于集合而不是成员,所以它作用于多个模型对象。 因此,路由应该是search_qtl_tables_path ,注意多个表。

qtl_table是一个模型,您想要搜索它们的集合,因此它可以使路径读起来像“搜索多个记录”。

编辑 :我主要担心的是你的rake routes不应该两次显示那些qtl_table路线。 你在routes.rb某个地方重复routes.rb吗?

好的,所以你删除了额外的路线。 现在,这应该工作……

 <%= form_tag search_qtl_table_index_path, :method => 'get' do %> 

尝试:

 Qtl::Application.routes.draw do resources :qtl_table do collection do get 'search', :as => 'search_qtl_table' end end ... end