rails,语法错误,意外','

时间:2012-08-12 19:08:32

标签: ruby-on-rails ruby-on-rails-3.2

我的搜索中有几个条件。

@events = Event.search(params[:search], 
  :conditions => {:group_size => 1, :days => 1})

上面的代码工作正常。但是,如果我想用私有方法替换哈希,我会收到语法错误

syntax error, unexpected ',', expecting tASSOC
    :conditions => {group_size_condition, :days => 1},

代码如下

@events = Event.search(params[:search], 
  :conditions => {group_size_condition, :days => 1})

private

def group_size_condition
  if params[:groupsize] == 'single (1)'
    :group_size => 1
  elsif params[:groupsize] == 'couple (2)'
    :group_size => 2
  elsif params[:groupsize] == 'small group(3-5)'
    :group_size => 3..5
  else
    nil
  end
end

提前致谢

3 个答案:

答案 0 :(得分:2)

那将是工作

@events = Event.search(params[:search], 
  :conditions => group_size_condition.merge(:days => 1))

private

def group_size_condition
  case params[:groupsize]
  when 'single (1)' then {:group_size => 1}
  when 'couple (2)' then {:group_size => 2}
  when 'small group(3-5)' then {:group_size => 3..5}
  else
    {}
  end
end

答案 1 :(得分:0)

我想你错过了:group_size key

@events = Event.search(params[:search], 
  :conditions => {:group_size => group_size_condition, :days => 1})

答案 2 :(得分:0)

我想也许你想要实际传递字符串,如:

def group_size_condition
  if params[:groupsize] == 'single (1)'
    ':group_size => 1'
  elsif params[:groupsize] == 'couple (2)'
    ':group_size => 2'
  elsif params[:groupsize] == 'small group(3-5)'
    ':group_size => 3..5'
  else
    nil
  end
end