Опитвам се да добавя формуляр за търсене на моята главна начална страница на моето приложение rails, който може да търси в кандидат модела, за да види, че ако напиша името на кандидат, той показва снимки на възможни кандидати и щракването върху тях получава към техните страници за кандидати.
Въпрос: формулярът за търсене се показва на началната страница, но след това получавам грешка при маршрутизиране: Няма маршрут, съответстващ на [GET] "/търсене"
Ето как изглежда кодът във 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