我正在尝试在我的 rails 应用程序的主页上添加一个搜索表单,该表单可以搜索候选模型,以查看如果我输入候选者的姓名,它会显示可能候选者的图片,然后单击它们会得到到他们的候选页面。
问题:搜索表单显示在主页上,但随后出现路由错误:No route matches [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