2016-03-24 15 views
1

従業員のドロップダウンリストを作成しました。コレクションのオブジェクトの値を取得する選択

  • 私は何をしたいですか?

それぞれの名前を完全に選択したいとします。フォームの

  • タイプ:

私はsimple_formを使用しています。私はcollection_selectを使用する前に

enter image description here

が、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") 

答えて

1

役立つこれを行う最も簡単な方法があると思う:

= f.select :person_id, options_for_select(@employees.map{|e| [e.fullName, e.id]}), {:prompt=>"Wybierz pracownika", :required => true} 

それは選択オプションとして、フルネームが表示され、値としてIDSを派遣しますフォームで。

+0

私はあなたのコードを使用すると、構文エラーがあります:予期しない '|'、expecting ')' –

+0

答えは今試してみてください! –

+0

正しく動作:f.select:person_id、options_for_select(@ employees.map {| e | [e.fullName、e.id]})、{:prompt => "Wybierz pracownika"、:required => true} –

1

あなたは、このコードの下のように従ってください。

<%= f.collection_select(:person_id, Model.all, :person_id, :fullName,{:prompt=>"Wybierz pracownika"}, {:class => 'form-control'}) %> 

あなたのMODEL_NAMEを交換します。

それとも

= f.input :person_id, label: "Employee", collection: @employee.fullName, prompt: "Select employee" 

私はあなた