if / else返回哈希值

时间:2013-04-16 12:29:28

标签: ruby ruby-on-rails-3 if-statement hash

我想对返回的哈希执行查询但是在哈希上得到未定义的方法< =。我的查询看起来像这样

<% 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 个答案:

答案 0 :(得分:2)

尝试Date.parse:

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