我想对返回的哈希执行查询,但在哈希上获得未定义的方法 <=。我的查询看起来像这样
<% 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 %>