思维狮身人面像搜索中的条件参数

时间:2012-11-19 14:07:51

标签: ruby-on-rails ruby-on-rails-3 sphinx params

我正在尝试将条件参数应用于思维狮身人面像搜索。

这就是我想要实现的(伪(ish)代码)。

我意识到这可能是一个愚蠢的问题,或者我是以错误的方式解决它。但任何帮助都会很棒。

DistributerGame.search :conditions => {:game_genre => @genre *** IF PARAMS[GENRE] EXISTS ***}, :with => {:distributer_id => @distributer_id *** if PARAMS[DISTRIBUTER] EXISTS ***, :price_in_gbp => @price  ***  if PARAMS[PRICE] EXISTS ***}

谢谢, 马特

1 个答案:

答案 0 :(得分:1)

你可以这样做:

conditions = {}
conditions[:game_genre] = @genre if params[:genre].present?
withs = {}
withs[:distibuter_id] = @distributer_id if params[:distibuter].present?
withs[:price_in_gbp] = @price if params[:price].present?
# ...
DistributerGame.search :conditions => conditions, :with => withs

坚持params不同页面

#in your view:   
filter_params = {}
filter_params[:game_genre] = params[:genre] if params[:genre].present?
filter_params[:distibuter] = params[:distibuter] if params[:distibuter].present?
filter_params[:price_in_gbp] = params[:price] if params[:price].present?

@distributers.each do |d|
  filters_params[:distibuter] = d.id
  link_to d.name, filter_params # you may have to merge this with your resource_path

@genres.each do |g|
  filter_params[:genre] = g.id
  link_to g.name, filter_params