我试图更改我在rails上的form_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>
关于我做错的任何想法?
答案 0 :(得分:3)
看起来您将:selected
键放在html_options参数属性中。
Here is the method definition for collection_select
:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
试试这个:
<%= f.collection_select :course_type_id, CourseType.where(:deleted => false), :id, :name, {:selected => @course_template.course_type.name}, {class: 'form-control m-b' } %>
答案 1 :(得分:1)
<%= f.collection_select :course_type_id, CourseType.where(:deleted => false), :id, :name, { :selected => @course_template.course_type.id }, {class: 'form-control m-b' } %>
selected
参数采用value
,而不是选项的name
。selected
选项和html_options
是不同的参数。如需进一步了解,请参阅here。