2016-05-27 9 views
0

私のフォームのcollection_selectで選択されているオプションを、レールで変更しようとしています。常に選択されたオプションは、最初のものを示し、決してユーザーが別のオプションを選択しない限り変更が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> 

任意のアイデア?

+0

'@ course_template.course_type.name'の値は何ですか? –

+0

'@ course_template.course_type.name'の値はこの場合' Pratical'です – InesM

+0

':selected => @ course_template.course_type.id'を試すことができますか? –

答えて

3

:selectedキーをhtml_options引数の属性に入れているようです。

Here is the method definition for collection_selectselectedパラメータはオプションのnamevalueを取り、ない

<%= f.collection_select :course_type_id, CourseType.where(:deleted => false), :id, :name, {:selected => @course_template.course_type.name}, {class: 'form-control m-b' } %> 
1

<%= f.collection_select :course_type_id, CourseType.where(:deleted => false), :id, :name, { :selected => @course_template.course_type.id }, {class: 'form-control m-b' } %>

  1. collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) 
    

    これを試してみてください。

  2. collection_select定義から、selectedオプションとhtml_optionsは異なるパラメータです。

hereを参照してください。

関連する問題