简体   繁体   中英

Rails 3 multi-part search

I've spent a few days on this and just haven't been able to get my head around how to apply a scope or other method to this. I've also tried some search gems but because I'm at such a basic level, wanted to keep things as simple as possible. The effort below is loosely based on rails casts #37 "simple search" tutorial. Its the only one that's been marginally successful.

My problem; I want to run a search on year, make, model, engine(if any), drilling down to the desired vehicle(s). To make things easy on me, I did one search per page. Currently I'm not able to carry the previous search params over to the current search. I'm sure there are better ways of doing this than the one below, and I'm open to suggestions. I just haven't been able to successfully adapt examples I've found online.

The Database: I have a postgres database table named "carinfo", that table has columns "id, caryear, carmake, carmodel, carengine, submodel, website1, website2, website3". I've populated it with some test data with a dozen or so vehicles.

controllers/listing_controller.rb

To simplify things I haven't used a model yet so its all in the controller:

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

views/listing/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>

views/listing/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>

views/listing/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>

views/listing/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>

views/listing/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 Just the section dealing with this problem:

  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'

As always, help is greatly appreciated. Thanks Mike

First off, you're controller doesn't need all of those methods, do something like this:

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

Then just have one form that has all of the inputs that you'd like to search

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM