Rails - 根据地理位置有条件地在索引视图上设置搜索参数

时间:2013-05-04 04:50:36

标签: ruby-on-rails

我有一个餐厅模型,对于索引路径,如果用户导航到/ restaurants,我想传入默认搜索参数。

我的用户界面基于搜索栏,但如果用户直接进入餐厅模型的索引视图,我不希望它为空,因为没有搜索到任何内容。

我正在使用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地理定位),因为其中一个搜索属性是餐厅的城市?

1 个答案:

答案 0 :(得分:0)

request.location.city

这应该包含用户城市。