【发布时间】:2013-04-11 22:12:56
【问题描述】:
我正在尝试在我的 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
【问题讨论】:
-
你有什么问题?
-
对不起,我编辑了一点!
-
看起来您可能正在使用家庭控制器,但搜索代码在您的搜索控制器中。也许将该搜索方法移至您的家庭控制器。需要将自己更改为候选人
-
我仍然遇到同样的路由错误。我尝试添加一条搜索路线,但不确定要匹配什么...
标签: ruby-on-rails search rails-routing