2012-01-04 69 views
0

Possible Duplicate:
Rails: Routing without plurals gives strange helpers扶手:路由和路徑傭工

原來我有:資源qtl_table在配置/ routes.rb中兩次!我得到這個錯誤:

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

在app /視圖/ qtl_table/index.html.erb:

<h2>Search for QTL</h2> 
<%= form_tag search_qtl_table_index, :method => 'get' do %> 
     <%= text_field_tag :search, params[:search] %> 
     <%= submit_tag "Search", :name => nil %> 
<% end %> 

和配置/ 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"} 
+0

步驟#1:與該定義的路由有關的'rake routes'的輸出是什麼? – tadman 2012-01-04 15:14:57

+0

請不要轉發您的問題。 – NullUserException 2012-01-06 02:42:06

回答

1

你可能有複數關閉,但這隻影響表名在數據庫中,Rails的不是如何處理路線。

因爲search路由屬於集合,而不是成員,它會作用於多個模型對象。所以,路線應該是search_qtl_tables_path,注意複數表。

qtl_table是一個模型,你想搜索它們的集合,所以它使感官的路線讀取像「搜索多個記錄」。

編輯:我主要關心的是您的rake routes不應該顯示那些qtl_table路線兩次。你是否在routes.rb的某個地方重複自己?

好的,所以你刪除了額外的路線。現在,這應該工作...

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

search_qtl_tables_path也沒有工作:( – bdeonovic 2012-01-04 15:19:42

+0

奇怪,[文檔](http://guides.rubyonrails .org/routing.html#adding-collection-routes)說這是預期的行爲。我修改了我的答案。 – Alex 2012-01-04 15:24:45

+0

原來我有:資源qtl_table config/routes.rb兩次!仍然收到錯誤和耙路線有一個奇怪的助手爲我的搜索方法。ANY想法? – bdeonovic 2012-01-04 16:06:01

0

嘗試:

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

沒有工作:(任何其他建議 – bdeonovic 2012-01-04 15:16:30

+0

您是否使用了'form_tag search_qtl_table_path,:method =>'get''? – clyfe 2012-01-04 16:48:40