Reputation: 1045
I am new to Rails and trying to filter search results by a drop down menu. The drop down is biketype (ie 'Road', 'Mountain'), which is an attribute of the bike model. I would then also like to sort by price.
My index view:
<% form_tag(bikes_path, :method => :get) do %>
<%= select_tag( :biketype, options_for_select(Bike::BIKETYPES) )%>
<%= submit_tag "Submit" %>
<% end %>
And my bikes_controller for the location-based search (used geocoder):
def index
@title = "Bikes"
if params[:search].present?
@bikes = Bike.near(params[:search], 50, :order => :distance).paginate(:page => params[:page], :per_page => 9)
else
@bikes = Bike.paginate(:page => params[:page], :per_page => 9)
end
end
If you had any suggestions as how to update my controller and model to get the filter and sort that would be great. I've been recommended scopes but not sure how to implement them. Thanks so much, Will.
Upvotes: 0
Views: 2517
Reputation: 15530
class BikesController < ApplicationsController
def index
@title = "Bikes"
@bikes = Bike.near_search(params[:search]).\
paginate(:page => params[:page], :per_page => 9)
end
end
class Bike
def self.near_search(params)
if params
near(params, 50, :order => :distance)
else
all
end
end
end
Upvotes: 1