Rails 3多部分搜索

时间:2012-12-20 17:15:09

标签: ruby-on-rails ruby-on-rails-3 search

我已经花了几天时间而已经无法理解如何应用范围或其他方法。我也尝试了一些搜索宝石,但因为我处于这样的基础水平,所以想让事情变得尽可能简单。下面的工作基于rails#37“简单搜索”教程。它是唯一一个勉强成功的人。

我的问题;我想对年份,品牌,型号,发动机(如果有的话)进行搜索,向下钻取到所需的车辆。为了方便我,我每页都进行了一次搜索。目前我无法将之前的搜索参数传递给当前搜索。我确信有比这更好的方法做到这一点,我愿意接受建议。我只是无法成功调整我在网上找到的例子。

数据库: 我有一个名为“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>

配置/ 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'

与往常一样,非常感谢帮助。 谢谢 麦克

1 个答案:

答案 0 :(得分:0)

首先,你是控制器不需要所有这些方法,做这样的事情:

class ListingController < ApplicationController 
  include ListingHelper 


  def search
    @search = Carinfo.where('caryear LIKE ?', params[:year]) unless params[:year].blank?
    @search = @search.where('carmake LIKE ?', params[:make]) unless params[:make].blank?
    etc etc...
  end

end

然后只有一个表单,其中包含您要搜索的所有输入