【问题标题】:how to set a default 'selected value' on a :collection dropdown/select textfield?如何在:collection 下拉/选择文本字段上设置默认的“选定值”?
【发布时间】:2012-07-09 18:26:23
【问题描述】:

我试图在下拉/选择文本字段中将某个值设为默认选择值。我已经尝试了很多选择都无济于事。谁知道如何根据 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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 forms collections simple-form


    【解决方案1】:

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

    尝试改变:

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

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

    【讨论】:

    • 试过没有用..这些表单问题非常令人沮丧,只是将一个值设置为默认值,这很难:(
    【解决方案2】:

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

    试试这个,

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

    【讨论】:

      【解决方案3】:

      是的,用这个。它有效:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-13
        • 2012-04-24
        • 2022-12-15
        • 2021-08-10
        • 2012-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多