我有一个Restaurants模型,对于索引路径,如果用户导航到/Restaurants,我想传入一个默认的搜索参数。
我的用户界面是基于搜索栏的,但是如果用户直接进入Restaurant模型的索引视图,我不希望它是空的,因为没有搜索到任何东西。
我使用Sunspot进行搜索,我的控制器代码看起来像这样:
empty_search = params[:search] ? false : true
if empty_search
@restaurants = Restaurant.all
else
@search = Sunspot.search(Restaurant) do
fulltext params[:search]
end
@restaurants = @search.results
end
但是,如果没有传入任何搜索参数,我不希望最终返回所有餐馆。
由于其中一个搜索属性是餐厅的城市,我将如何做一些事情,比如传入用户的城市(基于Geocoder地理位置)?
request.location.city