0

返されたハッシュに対してクエリを実行したいのですが、ハッシュで未定義のメソッド <= を取得します。私のクエリは次のようになります

<% 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 %>
4

1 に答える 1

2

Date.parse を試してください:

@fixture_date.select! { |key, hash| Date.parse(hash["fixture_date"]) <= (Date.today + 7.days) }
于 2013-04-16T12:45:32.397 に答える