gpt4 book ai didi

ruby-on-rails - RoR select_tag 默认值和选项

转载 作者:行者123 更新时间:2023-12-03 06:57:30 25 4
gpt4 key购买 nike

如何使用 select_tag 设置默认值,以及如何在页面加载时保持选项打开?

最佳答案

如果您在没有任何其他帮助程序的情况下使用select_tag,那么您可以在html中执行此操作:

select_tag "whatever", "<option>VISA</option><option selected=\"selected\">MasterCard</option>"

或者使用options_for_select:

select_tag "whatever", options_for_select([ "VISA", "MasterCard" ], "MasterCard")

或者使用options_from_collection_for_select:

select_tag [SELECT_FIELD_NAME], options_from_collection_for_select([YOUR_COLLECTION], [NAME_OF_ATTRIBUTE_TO_SEND], [NAME_OF_ATTRIBUTE_SEEN_BY_USER], [DEFAULT_VALUE])

示例:

select_tag "people", options_from_collection_for_select(@people, 'id', 'name', '1')

示例来自select_tag doc , options_for_select doc和来自 options_from_collection_for_select doc .

关于ruby-on-rails - RoR select_tag 默认值和选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3396025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com