0

특정 레코드를 선택하려고하는데 저장하려고 할 때 employee_id이 null 값으로 저장되고 정상적인 작동없이이 문제를 해결할 수있는 방법이 있습니까?Simple Form Association에서 값을 지정하십시오.

<%= f.association :employee , collection: 
Employee.where('student_advisor' => true).map(&:full_name) %> 

답변

2

이것은 나를 위해 일하는 것입니다 :

<%= f.association :employee, :collection => Employee.where('student_advisor' => true), :label_method => :full_name, :value_method => :id %> 

비록 이럴 그것의 더 나은 그래서

에 ... 귀하의 의견에서 논리를 유지하기 위해 컨트롤러에서 해당 쿼리를 설정합니다 컨트롤러 :

@employee_list = Employee.where('student_advisor' => true) 

그리고보기 :

<%= f.association :employee, :collection => @employee_list, :label_method => :full_name, :value_method => :id %> 
+0

여전히 employee_id를 null로 저장합니다. –

+0

정말요? html 소스를 보면 양식의 선택 항목에 값이 표시됩니까? 예 : '' –

+0

또한'f.association : employee'를'f.input : employee_id'로 바꾸고 콜렉션을 전달할 수도 있습니다. –

관련 문제