2013-04-16 50 views
0

我想对返回的散列执行查询,但得到未定义的方法< = on Hash。我的查询看起来像这样if/else返回的散列

<% if @fixture_date <= (Date.today + 7.days) %> 
Display fixtures whos date falls in this range 
<% else %> 
no fixtures 
<% end %> 

我有形式返回日期

<%= form_tag controller: 'predictions', action: 'create', method: 'post' do %> 
<% @fixture_date.sort.each do |date, fixture| %> 
<h5><%= date_format(date) %></h5><br> 
<% fixture.each do |fixture|%> 

<%= fixture.home_team %> vs <%= fixture.away_team %> 
<%= hidden_field_tag "predictions[][home_team]", fixture.home_team %> 
<%= hidden_field_tag "predictions[][away_team]", fixture.away_team %> 
<%= text_field_tag "predictions[][home_score]" %> 
<%= text_field_tag "predictions[][away_score]" %><br /> 

<%= hidden_field_tag "predictions[][fixture_date]", fixture.fixture_date %> 
<%= hidden_field_tag "predictions[][fixture_id]", fixture.id %> 
<%= hidden_field_tag "predictions[][user_id]", current_user.id %> 
<% end %> 
<%= submit_tag "Submit predictions" %> 
<% end %> 
<% end %> 

哈希的实例分组夹具返回

2013-04-17"=>[#<Fixture id: 3, home_team: "Man City", away_team: "Wigan", fixture_date: "2013-04-17", kickoff_time: "19:45", created_at: "2013-04-14 14:09:06", updated_at: "2013-04-14 14:09:06", prediction_id: nil>, #<Fixture id: 4, home_team: "West Ham", away_team: "Man Utd", fixture_date: "2013-04-17", kickoff_time: "19:45", created_at: "2013-04-14 14:09:06", updated_at: "2013-04-14 14:09:06", prediction_id: nil>, #<Fixture id: 5, home_team: "Fulham", away_team: "Chelsea", fixture_date: "2013-04-17", kickoff_time: "20:00", created_at: "2013-04-14 14:09:06", updated_at: "2013-04-14 14:09:06", prediction_id: nil>], 

我是否需要查询键的值,如果是的话,我会怎么做呢?我猜如果我得到一个数组返回然后这将工作?还是我误解了这个?

我当时想过在控制器这样做的,走到这一步

@fixture_date.select{ |key, hash| hash["fixture_date"] <= (Date.today + 7.days) } 

而我又似乎得到一个错误,这个时候

Cant convert String into Integer 

感谢

编辑

def new 
@prediction = Prediction.new 
@fixtures = Fixture.all 
@fixture_date = @fixtures.group_by {|fd| fd.fixture_date} 
@fixture_date.select{ |k, v| v['fixture_date'].to_date <= (Date.today + 7.days) } 
end 

查看

<%= form_tag controller: 'predictions', action: 'create', method: 'post' do %> 
<% @fixture_date.sort.each do |date, fixture| %> 
<h5><%= date_format(date) %></h5><br> 

<% fixture.each do |fixture|%> 

<%= fixture.home_team %> vs <%= fixture.away_team %> 
<%= hidden_field_tag "predictions[][home_team]", fixture.home_team %> 
<%= hidden_field_tag "predictions[][away_team]", fixture.away_team %> 
<%= text_field_tag "predictions[][home_score]" %> 
<%= text_field_tag "predictions[][away_score]" %><br /> 

<%= hidden_field_tag "predictions[][fixture_date]", fixture.fixture_date %> 
<%= hidden_field_tag "predictions[][fixture_id]", fixture.id %> 
<%= hidden_field_tag "predictions[][user_id]", current_user.id %> 
<% end %> 
<% end %> 
<%= submit_tag "Submit predictions" %> 
<% end %> 
+1

你得到它的权利。请给我们这个错误的行,但很可能它可以用'hash [“fixture_date”] .to_date'修复' – fotanus

+0

啊简单一点,然后我没有想到,将尝试.to_date方法,谢谢 – Richlewis

+0

@ fotanus好吧,所以我只是尝试了下面的答案以及添加to_date与我的原始代码,但所有的灯具都显示不属于我设置的条件。你能看到任何它不起作用的原因吗? – Richlewis

回答

2

尝试Date.parse:

@fixture_date.select! { |key, hash| Date.parse(hash["fixture_date"]) <= (Date.today + 7.days) } 
+0

这和.to_date有什么区别?谢谢你的回答 – Richlewis

+0

to_date调用Date.parse,所以两者都有效。 –

+0

好吧,所以我只是尝试了你的答案以及添加to_date与我的原始代码,但所有的灯具都显示不属于我设置的条件。你能看到任何它不起作用的原因吗? – Richlewis