开发者

Drop Down Search Results Filter in Rails

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]开发者_JAVA技巧, :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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜