本文介绍了options_for_select 选定的值不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个projectuser 数据表单,但是当我编辑projectuser 时,选中的projectuser 保持为空,这是怎么回事?这是代码:

I have a form for projectuser data, but when I edit the projectuser, the selected projectuser stays empty, what is going wrong? This is the code:

= f.select :user_id, options_for_select(@users.map{ |u| [u.full_name, u.id] }, selected: @projectuser.full_name), include_blank: true

推荐答案

文档说明:

(...) 
options_for_select(container, selected = nil) public
(...)
(million examples)

所以你不应该传递selected: something,而只是something.此外,这需要选择值,而不是文本:

So you shouldn't pass selected: something, but just something. Also, this something needs to be selected value, not text:

= f.select :user_id, options_for_select(@users.pluck(:name, :id), @projectuser.id), include_blank: true

这篇关于options_for_select 选定的值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 12:18