错误描述:需要在项目中建立一个简单的搜索,待搜索模型为 topics,代码描述如下:
#_search.html.erb
<form class="form-inline">
<%= form_tag search_topics_path, method: :get do %>
<%= text_field_tag :search %>
<%= submit_tag "搜索" %>
<% end %>
</form>
#config/routes.rb
resources :topics do
get :search, on: :collection
end·
#topics_controller.rb
class TopicsController < ApplicationController
def search
search = params[:search].present? ? params[:search] : nil
@topics = if search
Topic.where("title LIKE ?", "%#{search}%").order("created_at desc").page(params[:page])
else
Topic.all.order("created_at desc").page(params[:page])
end
debugger
end
end
将_search.html.erb 放置在 welcomes/index 这个页面中,点击如上的搜索按钮,回到的是 welcomes 的 index 中,但是我想要讲参数送到到 topics 中 search 中,哪里出现了问题?