【问题标题】:f.select not preselecting value while editingf.select 编辑时不预选值
【发布时间】:2016-08-29 11:24:49
【问题描述】:

我有一个字段 state_id 我正在使用选择下拉菜单:

= form.select :state_id, options_for_select(states), {}, prompt: 'Select State'

但在编辑表单时,它不会预先选择state_id 的当前值。我知道我可以通过:selected 参数来传递默认选择值:

options_for_select(states, @object.state_id)

但是正如guide 所暗示的,我不需要使用这些选项。

谁能告诉我我做错了什么?

注意:这是一个嵌套属性,其他字段可以正常使用 f.text_field,但不能使用 f.select

【问题讨论】:

  • 我不确定,但prompt: 会覆盖选择吗?
  • states - 这会返回什么?
  • 状态 = [[ "Alabama", 1 ], [ "Alaska", 2 ], [ "Arizona", 3 ]]
  • @ollpu 不,我也只尝试了= form.select :state_id, options_for_select(states)
  • 似乎有一个类似的问题:stackoverflow.com/questions/20497503/…,其中您只是将状态称为第二个参数,没有 options_for_select。如果这不起作用,您可以分享您的相关控制器和表单吗?

标签: ruby-on-rails haml form-helpers


【解决方案1】:

你可以试试这个。

= select_tag "state_id", options_from_collection_for_select(states, "id", "name",@object.state_id), prompt: "Select State", placeholder: "Select State"

希望它能帮到你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    相关资源
    最近更新 更多