在简单的表单协会指定的值 [英] specify values in Simple Form Association

查看:163
本文介绍了在简单的表单协会指定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择特定的记录,但是当试图保存 EMPLOYEE_ID 保存用null值,并没有把它正常工作时,任何情况下,有什么办法解决此问题?

 <%= f.association:员工,收藏:
Employee.where('student_advisor'=>真).MAP(安培;信息:full_name)%GT;


解决方案

这对我来说是什么作品:

 <%= f.association:员工:收集=> Employee.where('student_advisor'=>真):label_method => :FULL_NAME,:value_method => :ID%GT;

虽然恕我直言,它能够更好地设置在控制器的查询,以保持逻辑出你的看法......

因此​​,在控制器:

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

和视图:

 <%= f.association:员工:收集=> @employee_list,:label_method => :FULL_NAME,:value_method => :ID%GT;

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) %>

解决方案

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 %>

这篇关于在简单的表单协会指定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆