我对表单选择存在疑问。我正在混合Bootstrap和Rails,我有一个不起作用的选择。该错误表示“没有将String隐式转换为整数”。我不知道为什么,因为我一直在寻找Stackoverflow,我已经在我的代码中添加了相同的内容。
<%= form_for @control, :html => {:class => 'form-inline'} do |f| %>
<div class="form-group">
<div class="input-group">
<%= f.label :level, class: "sr-only", for: "exampleInputEmail2" %>
<%= f.number_field :level, class: "form-control", id: "exampleInputEmail2", placeholder: "Enter level" %>
</div>
</div>
<div class="form-group container">
<div class="row datetimeselect">
<div class='col-sm-12'>
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<%= f.text_field :day, default: DateTime.current, :class => 'form-control' %>
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
<select class="form-group">
<div class="input-group">
<%= f.select :period, options_for_select([['pre-breakfast', 1], ['post-breakfast', 2],['pre-lunch', 3]['post-lunch', 4]['afternoon', 5]['pre-dinner', 6]['post-dinner', 7]]), {}, class: "form-control" %>
</div>
</select>
<%= f.submit "Save", type:"submit", class: "btn btn-default" %>
<% end %>
答案 0 :(得分:3)
options_for_select()
方法中的问题,您遗漏了,
:
# your code
<%= f.select :period, options_for_select([['pre-breakfast', 1],
['post-breakfast', 2],
['pre-lunch', 3] <----- first missing comma
['post-lunch', 4] <----- again
['afternoon', 5] <----- again
['pre-dinner', 6] <----- again
['post-dinner', 7]]), {}, class: "form-control" %>
只需将,
放在所有数组之后,这应该可以解决您的问题。
应该在一行中!
# valid syntax
<%= f.select :period, options_for_select([['pre-breakfast', 1],
['post-breakfast', 2],
['pre-lunch', 3],
['post-lunch', 4],
['afternoon', 5],
['pre-dinner', 6],
['post-dinner', 7]]), {}, class: "form-control" %>