【发布时间】:2016-05-27 19:58:05
【问题描述】:
我正在尝试更改在我的 rails 表单上的 collection_select 上选择的选项。
我的代码如下所示:
<%= f.collection_select :course_type_id, CourseType.where(:deleted => false), :id, :name, {}, {class: 'form-control m-b', :selected => @course_template.course_type.name } %>
但是,选择的选项始终显示第一个并且永远不会改变,除非用户选择不同的选项。
生成的 html 如下所示:
<select class="form-control m-b" selected="selected" name="course[course_type_id]" id="course_course_type_id">
<option value="1">Driving</option>
<option value="2">Pratical</option>
</select>
关于我做错了什么有什么想法吗?
【问题讨论】:
-
@course_template.course_type.name的值是多少? -
@course_template.course_type.name的值在这种情况下是Pratical -
你能试试
:selected => @course_template.course_type.id吗? -
我也试过像
<%= f.collection_select :course_type_id, CourseType.where(:deleted => false), :id, :name, {}, {class: 'form-control m-b', selected: @course_template.course_type.id } %>这样的id,但结果是一样的。 -
请尝试:
<%= f.collection_select :course_type_id, CourseType.where(:deleted => false), :id, :name, { :selected => @course_template.course_type.id }, {class: 'form-control m-b' } %>
标签: ruby-on-rails forms