我想對返回的散列執行查詢,但得到未定義的方法< = 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 %>
你得到它的權利。請給我們這個錯誤的行,但很可能它可以用'hash [「fixture_date」] .to_date'修復' – fotanus
啊簡單一點,然後我沒有想到,將嘗試.to_date方法,謝謝 – Richlewis
@ fotanus好吧,所以我只是嘗試了下面的答案以及添加to_date與我的原始代碼,但所有的燈具都顯示不屬於我設置的條件。你能看到任何它不起作用的原因嗎? – Richlewis