2013-04-11 29 views
1

我試圖在我的rails應用程序的主要主頁上添加一個搜索表單,它可以搜索整個候選人模型,看看如果我輸入候選人的名字,它會顯示可能的候選人的圖片,然後單擊他們進入他們的候選人頁面。Rails搜索表單 - 不知道如何讓它工作?

問題:在主頁上的搜索表單顯示,但後來我得到一個路由錯誤:沒有路由匹配[GET]「/搜索」

下面是代碼是什麼樣子的意見/首頁/指數.html.erb:

<%= form_tag("/search", :method => "get") do %> 
<%= text_field_tag(:q) %> 
<%= submit_tag("Search") %> 
<% end %> 

搜索控制器:

def index 
@candidates = Candidate.search(params[:name]) 
end 

搜索視圖(視圖/搜索/ index.html.erb):

<h1> Here are your search results: </h1> 
<% @candidates.each do |candidate| %> 
<%= link_to image_tag(candidate.picture, :size => "100x100", :alt => "Edit Entry"),  candidate%> 
<% end%> 

候選模型:

def self.search(name) 
where('name LIKE ?', "%#{name}%") 
end 

耙路線:

candidates_show GET /candidates/show(.:format)  candidates#show 
candidates_new GET /candidates/new(.:format)  candidates#new 
donations_index GET /donations/index(.:format)  donations#index 
home_index GET /home/index(.:format)   home#index 
import_candidates POST /candidates/import(.:format) candidates#import 
candidates GET /candidates(.:format)   candidates#index 
POST /candidates(.:format)   candidates#create 
new_candidate GET /candidates/new(.:format)  candidates#new 
edit_candidate GET /candidates/:id/edit(.:format) candidates#edit 
candidate GET /candidates/:id(.:format)  candidates#show 
PUT /candidates/:id(.:format)  candidates#update 
DELETE /candidates/:id(.:format)  candidates#destroy 
root  /       home#index 
+0

什麼是你的問題? –

+0

對不起,我編輯了一下! – girlrockingguna

+0

看起來您可能使用的是家用控制器,但搜索代碼位於您的搜索控制器中。也許將該搜索方法移至您的家庭控制器。將需要改變自己到候選人 –

回答

1

您的形式標記指向search但沒有叫search路線。就像上面所建議的,無論是從您的搜索控制器移動index方法到您的家庭控制器或添加search路線在你的routes.rb像

match "search" => "search#index" 
+0

我仍然得到相同的路由錯誤。我嘗試添加一個搜索路線,但不知道該匹配什麼...... – girlrockingguna

+0

現在你能告訴我「耙路線」的輸出嗎? –

+0

它的工作!問題:搜索對篩選候選人沒有效果(它只是顯示所有人的照片)。有什麼我正在做的SQL命令? – girlrockingguna