尝试这样的事情:
<% cweek = Date.today.cweek
sms =StockMovement.where(week: (cweek..cweek+3)).order("year DESC, week DESC")
sms_now = sms.last.id
%>
<%= select_tag("stock_movement", options_for_select(sms.map { | val | [ "#{ val.year }/#{ val.week }", val.id] }, :selected => sms_now)) %>
如果再摆弄一下,这是另一个解决方案,它使用 form.select 帮助器并在模型中预选包含参考的值(在我的情况下为研讨会)(如果存在),否则为当前周。
<% cweek = Date.today.cweek
sms =StockMovement.where(week: (cweek..cweek+3)).order("year DESC, week DESC")
sms_selected = @seminar.stock_movement ? @seminar.stock_movement.id : StockMovement.where(week: cweek).first.id
%>
<%= f.select("stock_movement_id", sms.map { | val | [ "#{ val.year }/#{ val.week }", val.id] },{:selected => sms_selected}) %>