我有一个搜索表单,用于搜索并显示要列出的搜索结果。
列表架构是:
t.string "title"
t.text "description"
t.string "city"
t.string "state"
t.string "zipcode"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "category_id"
t.integer "subcategory_id"
t.float "latitude"
t.float "longitude"
t.integer "user_id"
t.string "image"
t.integer "year"
t.integer "miles"
t.string "transmission"
t.string "color"
t.string "cylinder"
t.string "fuel"
t.string "drive"
t.string "address"
t.boolean "wholesale", default: false
t.integer "price", default: 0
t.string "newused"
t.string "vin"
t.string "stocknumber"
t.string "model"
t.string "trim"
t.string "enginedescription"
t.string "interiorcolor"
t.string "options"
t.string "imagefront"
t.string "imageback"
t.string "imageleft"
t.string "imageright"
t.string "frontinterior"
t.string "rearinterior"
t.string "bodytype"
t.string "make"
我的搜索表单使用户能够搜索newused,make,model,price,near和model。
<div id="Make" class="tabcontent1">
<section class="formclass">
<!-- f.select :transmission, ['Automanual','Automatic','Automatic 4 Speed','Automatic 5 Speed','Automatic 6 Speed','CVT','Manual'] -->
<h3 style = "color:#F00000; text-align: center;"><strong><%= @carcount %> CARS LISTED!</strong></h3>
<hr>
<h3 style = "color:#7C064D;"><span class="glyphicon glyphicon-search"></span> <strong>SEARCH CARS FOR SALE BY MAKE</strong></h3>
<%= form_tag search_listings_path, method: :get, class: 'navbar-form navbar-center' do |f| %>
<div class= "col-xs-12 col-sm-12 col-lg-12 col-md-12">
<%= select_tag :NewUsed, "<option>New</option><option>Used</option>".html_safe, style: "width: 100%; margin: 1% 0;" %>
</div>
<div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6">
<%= select_tag :category, include_blank: true, style: "width: 100%; margin: 1% 0;" %>
</div>
<div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6">
<%= select_tag :subcategory, include_blank: true, style: "width: 100%; margin: 1% 0;" %>
</div>
<!-- <div class= "col-xs-12 col-sm-12 col-lg-12 col-md-12"> -->
<div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6">
<%= text_field_tag :minprice, nil, placeholder: 'Min Price', style: "width: 100%; margin: 1% 0;" %>
</div>
<div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6">
<%= text_field_tag :maxprice, nil, placeholder: 'Max Price', style: "width: 100%; margin: 1% 0;" %>
</div>
<!-- </div> -->
<div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6">
<%= text_field_tag :location, nil, placeholder: 'Near', style: "width: 100%; margin: 1% 0;" %>
</div>
<div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6">
<%= text_field_tag :radius, nil, placeholder: 'Radius', style: "width: 100%; margin: 1% 0;" %>
</div>
<div class= "col-xs-12 col-sm-12 col-lg-12 col-md-12">
<%= submit_tag 'Search', class: 'btn btn-danger', style: "width: 100%;" %>
</div>
<% end %>
</section>
</div>
搜索是Listing模型中的基本搜索方法:
def self.search(params)
if params
listings = Listing.all
listings = listings.joins(:category).where("categories.name like '#{params[:category].downcase}'") if params[:category].present?
listings = listings.joins(:subcategory).where("subcategories.name like '#{params[:subcategory].downcase}'") if params[:subcategory].present?
listings = listings.where("listings.NewUsed = '#{params[:NewUsed][0].upcase}'") if params[:NewUsed].present?
listings = listings.where("price >= ?", "#{params[:minprice]}") if params[:minprice].present?
listings = listings.where("price <= ?", "#{params[:maxprice]}") if params[:maxprice].present?
if params[:radius].present?
listings = listings.near(params[:location],params[:radius]) if params[:location].present?
else
listings = listings.near(params[:location],200) if params[:location].present?
end
listings
else
all
end
end
用于所有航空公司,酒店,汽车销售网站。请帮帮我,或者至少向我展示一些方向。