<%= form_for(@timetable) do |f| %>
<% Timetable.all.each do |t| %>
<% if t.day == "Monday" %>
<%= f.select :day, options_for_select(%w[Tuesday Wednesday Thursday Friday Saturday Sunday]) %>
<% elsif t.day == "Tuesday" %>
<%= f.select :day, options_for_select(%w[Wednesday Thursday Friday Saturday Sunday]) %>
<% elsif t.day == "Wednesday" %>
<%= f.select :day, options_for_select(%w[Thursday Friday Saturday Sunday]) %>
<% elsif t.day == "Thursday" %>
<%= f.select :day, options_for_select(%w[Friday Saturday Sunday]) %>
<% elsif t.day == "Friday" %>
<%= f.select :day, options_for_select(%w[Saturday Sunday]) %>
<% elsif t.day == "Saturday" %>
<%= f.select :day, options_for_select(%w[Sunday]) %>
<% else %>
<%= f.select :day, options_for_select(%w[Monday Tuesday Wednesday Thursday Friday Saturday Sunday]) %>
<% end %>
<% end %>
<% end %>
如果值“Monday”在数据库字段“day”中退出,则值“Monday”不应出现在选择选项中。有没有其他有效的方法来做到这一点?
得到了答案
<% @values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] %>
<%= f.select :day, options_for_select(@values - Timetable.pluck(:day)) %>
答案 0 :(得分:0)
<% Timetable.all.each do |t| %>
<%= f.select :day, options_for_select(%w[Sunday Saturday Friday Thursday Wednesday Tuesday Monday].take_while { |day| day != t.day }.reverse!, selected: @timetable.day ) %>
<% end %>
答案 1 :(得分:0)
<% values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] %>
<%# Timetable.all.each do |t| %>
<%= f.select :day, options_for_select(values - [@timetable.day], selected: @timetable.day ) %>
<%# end %>
我注释掉了循环,因为显示了多个选择框。