快速提问。我正在尝试设置一个使用开始日期、结束日期和关键字搜索数据库的页面。如果我要搜索 19-07-2012,路径看起来像
search?utf8=%E2%9C%93&project%5Bstart_date%5D=19-07-2012&project%5Bend_date%5D=&%5Bstatus%5D=&keywords=
所有的结果都会显示出来。但是,当我搜索关键字时,搜索工作。我看不出我哪里错了。
<h1>Search</h1>
<%= form_tag search_path, method: :get do %>
Start Date :
<%#= text_field_tag :start_date, params[:start_date] %></br>
<div class="field">
<%= datepicker_input "project","start_date" %></br>
</div>
End Date :
<%#= text_field_tag :end_date, params[:end_date] %></br>
<%= datepicker_input "project","end_date" %></br>
Status :
<%= select(@projects, :status, Project.all.map {|p| [p.status]}.uniq, :prompt => "-Any-", :selected => params[:status]) %></br>
Keywords :
<%= text_field_tag :keywords, params[:keywords] %></br>
<%= submit_tag "Search", name: nil %>
<% end %>
控制器:
def search
@project_search = Project.search(params[:start_date], params[:end_date], params[:keywords]).order(sort_column + ' ' + sort_direction).paginated_for_index(per_page, page)
@project = Project.new(params[:project])
respond_to do |format|
format.html # search.html.erb
format.json { render :json => @project }
end
end
编辑:
当我使用字符“q”作为关键字进行搜索时,开始日期为 25-07-2012,sql 看起来像这样
Parameters: {"utf8"=>"✓", "project"=>{"start_date"=>"25-07-2012", "end_date"=>""}, "status"=>"", "keywords"=>"q"}
Project Load (0.4ms) SELECT "projects".* FROM "projects"
(0.2ms) SELECT COUNT(*) FROM "projects" WHERE (client LIKE '%%' AND industry LIKE '%%' AND role LIKE '%%' AND tech LIKE '%%' AND business_div LIKE '%%' AND project_owner LIKE '%%' AND exception_pm LIKE '%%' AND status LIKE '%%' AND start_date LIKE '%%' AND end_date LIKE '%%' AND keywords LIKE '%q%')
这似乎只搜索关键字“q”。当我删除关键字并保留日期时,sql 看起来像:
Parameters: {"utf8"=>"✓", "project"=>{"start_date"=>"25-07-2012", "end_date"=>""}, "status"=>"", "keywords"=>""}
Project Load (0.3ms) SELECT "projects".* FROM "projects"
提前感谢您提供的任何帮助!