我正在尝试在我的rails应用主页上添加一个搜索表单,该搜索表单可以搜索候选模型,看看如果我输入候选人的名字,它会显示可能的候选人的照片,然后点击他们到他们的候选页面。
问题:搜索表单显示在主页上,但后来我收到路由错误:没有路由匹配[GET]“/ search”
以下是views / home / index.html.erb中的代码:
<%= form_tag("/search", :method => "get") do %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
搜索控制器:
def index
@candidates = Candidate.search(params[:name])
end
搜索视图(views / search / 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 :(得分:1)
您的表单标记指向search
,但没有名为search
的路由。与上面建议的一样,要么将index
方法从搜索控制器移动到您的家庭控制器,要么在您的routes.rb中添加search
路径,如
match "search" => "search#index"