1

我试图在下拉/选择文本字段中将某个值设为默认选择的值。我已经尝试了很多选择都无济于事。谁知道如何根据 Profile 模型中的值设置默认值?

= f.input_field :state_id,
                :label => "Your state",
                :collection => DataState.all,
                :default =>  [DataState.where(:id => @user.profile.state_id)],
                :style => "width: 110px !important"

我使用 simple_form

4

3 回答 3

2

您是否尝试过使用该:selected选项?

尝试更改:

:default =>  [DataState.where(:id => @user.profile.state_id)],

为了

:selected =>  [DataState.where(:id => @user.profile.state_id)],
于 2012-07-09T20:07:59.127 回答
1

我认为,您必须提供 ID# 而不是整个对象

试试这个,

:default =>  [DataState.where(:id => @user.profile.state_id).id],
于 2012-07-10T06:51:07.853 回答
1

是的,用这个。有用:

:selected =>  [DataState.where(:id => @user.profile.state_id).first.id],
于 2013-01-02T17:24:21.563 回答