我是Rails的新手并尝试通过下拉菜单过滤搜索结果。下拉是biketype(即'Road','Mountain'),这是自行车模型的属性。我还想按价格排序。
我的索引视图:
<% form_tag(bikes_path, :method => :get) do %>
<%= select_tag( :biketype, options_for_select(Bike::BIKETYPES) )%>
<%= submit_tag "Submit" %>
<% end %>
我的bikes_controller用于基于位置的搜索(使用地理编码器):
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
如果你有任何建议,如何更新我的控制器和模型,以获得过滤器和排序,这将是伟大的。我被推荐范围,但不知道如何实现它们。非常感谢,Will。
答案 0 :(得分:1)
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