従業員のドロップダウンリストを作成しました。コレクションのオブジェクトの値を取得する選択
- 私は何をしたいですか?
それぞれの名前を完全に選択したいとします。フォームの
- タイプ:
私はsimple_formを使用しています。私はcollection_selectを使用する前に
が、simple_formは、コレクションのこのタイプの検証をサポートしていません。
= f.input :person_id, label: "Employee", collection: @employee, prompt: "Select employee"
の検索結果を(私が知っている、それが参照される): は、私が実際に持っています。 collection_selectのコード。このタイプのドロップダウンリストでは、フルネームが正しく表示されます。
= f.collection_select :person_id, @employee, :id, :fullName, {prompt: "Wybierz pracownika"}, {class: "form-control"}
更新: のfullNameはperson.rbモデル内のメソッドです。
def fullName
"#{first_name} #{last_name}"
end
オブジェクト従業員。
@employee = Person.where.not(type: "Client")
私はあなたのコードを使用すると、構文エラーがあります:予期しない '|'、expecting ')' –
答えは今試してみてください! –
正しく動作:f.select:person_id、options_for_select(@ employees.map {| e | [e.fullName、e.id]})、{:prompt => "Wybierz pracownika"、:required => true} –