我已经花了几天的时间来解决这个问题,只是无法弄清楚如何将范围或其他方法应用于此。我也尝试了一些搜索宝石,但因为我处于如此基本的水平,所以想让事情尽可能简单。下面的工作大致基于 rails casts #37 “simple search” 教程。它是唯一一个略微成功的。
我的问题; 我想对年份、品牌、型号、发动机(如果有)进行搜索,深入到所需的车辆。为了方便我,我每页进行一次搜索。目前我无法将以前的搜索参数带到当前搜索中。我确信有比下面的方法更好的方法,我愿意接受建议。我只是无法成功地改编我在网上找到的例子。
数据库:我有一个名为“carinfo”的 postgres 数据库表,该表包含“id、caryear、carmake、carmodel、carengine、submodel、website1、website2、website3”列。我已经用十几辆左右的车辆填充了一些测试数据。
控制器/listing_controller.rb
为了简化事情,我还没有使用模型,所以它全部在控制器中:
class ListingController < ApplicationController
include ListingHelper
def year
@searchyr = Carinfo.find(:all)
end
def make
@searchmk = Carinfo.find(:all, :conditions => ['caryear LIKE ?', "%#{params[:year]}%"])
end
def model
@searchmdl = Carinfo.find(:all, :conditions => ['caryear LIKE ? AND carmake LIKE ?', "%#{params[:year]}%", "%#{params[:make]}%"] )
# '%2009%' static values work ok
end
def engine
@searcheng = Carinfo.find(:all, :conditions => ['caryear LIKE ? AND carmake LIKE ? AND carmodel LIKE ?', "%#{params[:year]}%", "%#{params[:make]}%", "%#{params[:model]}%"])
end
def selectedcar
@searchres = Carinfo.find(:all, :conditions => ['caryear LIKE ? AND carmake LIKE ? AND carmodel LIKE ? AND carengine LIKE ?', "%#{params[:year]}%", "%#{params[:make]}%", "%#{params[:model]}%","%#{params[:engine]}%"])
end
end
意见/清单/year.html.erb
<% provide(:title, "Year") %>
<h1>Year Search - Under: Listing</h1>
<%= form_tag(make_path, :method => "get") do %>
<%= select_tag :year, options_for_select(@searchyr.collect{|y| [y.caryear, y.caryear]}.uniq), {:onchange => 'this.form.submit()'} %>
<% end %>
<!-- this can be deleted once everything is working it just helps see what's filtered -->
<ul>
<% for search in @searchyr %>
<li>
<%= search.caryear %>
<%= search.carmake %>
<%= search.carmodel %>
</li>
<% end %>
</ul>
<center><%= button_to "Reset Search", year_path, class: "btn btn-large btn-primary" %></center>
意见/清单/make.html.erb
<% provide(:title, "Make Search") %>
<h1>Make Search - Under: Listing</h1>
<%= form_tag(model_path, :method => "get") do %>
<%= select_tag :make, options_for_select(@searchmk.collect{|y| [y.carmake, y.carmake]}.uniq), {:onchange => 'this.form.submit()'} %>
<% end %>
<center><%= button_to "Reset Search", year_path, class: "btn btn-large btn-primary" %></center>
意见/清单/model.html.erb
<% provide(:title, "Model Search") %>
<h1>Model Search - Under: Listing</h1>
<%= form_tag(engine_path, :method => "get") do %>
<%= select_tag :model, options_for_select(@searchmdl.collect{|y| [y.carmodel, y.carmodel]}.uniq), {:onchange => 'this.form.submit()'} %>
<% end %>
<center><%= button_to "Reset Search", year_path, class: "btn btn-large btn-primary" %></center>
意见/清单/model.html.erb
<% provide(:title, "Engine Search") %>
<h1>Engine Search - Under: Listing</h1>
<%= form_tag(selectedcar_path, :method => "get") do %>
<%= select_tag :engine, options_for_select(@searcheng.collect{|y| [y.carengine, y.carengine]}.uniq), {:onchange => 'this.form.submit()'} %>
<% end %>
<center><%= button_to "Reset Search", year_path, class: "btn btn-large btn-primary" %></center>
意见/清单/selectedcar.html.erb
<% provide(:title, "Selected Car") %>
<h1>Selected Car - Under: Listing</h1>
<center><%= button_to "Reset Search", year_path, class: "btn btn-large btn-primary" %></center>
<ul>
<% for search in @searchres %>
<li>
<%= search.caryear %>
<%= search.carmake %>
<%= link_to search.carmodel, search_path(search) %>
</li>
<% end %>
</ul>
config/routes.rb 只是处理这个问题的部分:
match '/year', to: 'listing#year'
match '/make', to: 'listing#make'
match '/model', to: 'listing#model'
match '/engine', to: 'listing#engine'
match '/selectedcar', to: 'listing#selectedcar'
一如既往,非常感谢您的帮助。谢谢迈克