1

我正在尝试在我的 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
4

1 回答 1

1

您的表单标签指向search但没有名为 的路由search。就像上面建议的那样,要么将index方法从你的搜索控制器移动到你的主控制器,要么search在你的 routes.rb 中添加路由,比如

match "search" => "search#index"
于 2013-04-11T22:29:19.910 回答