specify values in Simple Form Association

假如想象 提交于 2019-12-13 06:16:44

问题


I am trying to select specific record but when trying to save , employee_id is saved with a null value, and without putting any condition it works normally , is there any way to solve this ?

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

回答1:


This is what works for me:

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

Though IMHO its better to set that query in your controller to keep the logic out of your views...

So in the controller:

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

And in the view:

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


来源:https://stackoverflow.com/questions/19262954/specify-values-in-simple-form-association

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!